Home » Python » List in Python

About Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.

List in Python

In this article, we will explain List in Python through examples.

1. Introduction

Lists are used to store multiple items in a single variable. List items are indexed, the first item has index [0], the second item has index [1], etc. Also, a list is ordered, which means that the items have a defined order, and changeable, meaning that we can change, add and remove items in a list after it has been created.

2. Create a List and Access Its Elements

Lists are created using square brackets []. List items are indexed and you can access them by referring to the index number. The first item has an index of 0. Furthermore, we can use negative indexing (-1 refers to the last item, -2 refers to the second-last item, etc). You can specify a range of indexes by specifying where to start(included) and where to end(not included) the range. When specifying a range, the return value will be a new list with the specified items. Also, this is how you can slice a list.

pythonList.py
myList = ["car", "banana", "UFO", "red", 666]
print(myList)
print(myList[1])
print(myList[-1])
print(myList[2:4])
python list - First Example Output
Fig. 1: First Example Output

3. Add/Change List Elements

To add an item to the end of the list, use the append() method, and to insert an item at a specified index, use the insert() method. Also, to append elements from another list to the current list, use the extend() method. Last but not least, we can change the value of a specific item, refer to the index number.

pythonList.py
myList = ["car", "banana", "UFO", "red", 666]
myList[2] = 50
print(myList)
myList.insert(2, "inserted")
print(myList)
myList.append(True)
print(myList)
tmpList = [60, False, "papaya"]
print(tmpList)
myList.extend(tmpList)
print(myList)
python list - Second Example Output
Fig. 2: Second Example Output

4. Delete/Remove List Elements

The remove() method removes the specified item. The pop() method removes the specified index. If you do not specify the index, the pop() method removes the last item. The del keyword also removes the specified index. The del keyword can also delete the list completely.

pythonList.py
myList = ["car", "banana", "UFO", "red", 666]
myList.insert(0, "banana")
print(myList)
myList.remove("banana")
print(myList)
myList.pop(1)
print(myList)
del myList
python list - Third Example Output
Fig. 3: Third Example Output

5. Summary

As we can see from the examples above, lists are a very useful and simple way to store data in a sequence. It is more akin to an array in other programming languages. More list methods can be found here.

6. Download Source Code

This was an example of how we can create, parse, modify and delete a list in python.

Download
You can download the full source code of this example here: List in Python

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments