Python List with examples

Spread the love

What is a List in Python?

A list is a collection of items in a particular order. You can put anything inside a list like integers, floats, strings or even list inside of a list.

Create a List in Python –

In Python lists are represented by square brackets ( [ ] ) and all the individual elements inside the list is separated by commas.

Here is an example of a list in python.

companies = ['Google','Apple','Facebook','Microsoft','Netflix']

You can also create an empty list like this

In [2]: empty_list = []

In [3]: type(empty_list)
Out[3]: list

You can also have a list inside of a list.

lst = [5, 7, 2, [3, 'brad'], 10]

Accessing elements of a list in python –

Lists are ordered collections, so you can access any elements of the list by it’s position or index. To access an element in the list, write the name of the list followed by the index of the item enclosed in square brackets.

Let’s say you want to access the first element from the companies list.

In [5]: companies[0]
Out[5]: 'Google'

Index positions starts at 0, Not 1 –

Python considers the first item in the list to be at position 0, not position 1.

In [6]: # get the first item from the list

In [7]: companies[0]
Out[7]: 'Google'

In [8]: # get the second item from the list

In [9]: companies[1]
Out[9]: 'Apple'

The second item in a list has a index of 1. Using this counting system, you can get any element you want from a list by subtracting one from its position in the list. For instance to access the 4th item from the list, you request the item at index 3.

In [10]: companies
Out[10]: ['Google', 'Apple', 'Facebook', 'Microsoft', 'Netflix']

In [11]: companies[3]
Out[11]: 'Microsoft'

Negative Indexing –

You can also use negative indexing with python. To get the last item from a list you write lst[-1] and to get the second last item from the list you write lst[-2].

In [12]: # get the last item using negative indexing

In [13]: companies[-1]
Out[13]: 'Netflix'

In [14]: # get second last item

In [15]: companies[-2]
Out[15]: 'Microsoft'

List Slicing in Python –

You can also get multiple items from a list using list slicing. The syntax of list slicing is

list[start:end]

When we do list slicing the start index is inclusive but the end index is exclusive.

In [16]: companies
Out[16]: ['Google', 'Apple', 'Facebook', 'Microsoft', 'Netflix']

In [17]: companies[0:4]
Out[17]: ['Google', 'Apple', 'Facebook', 'Microsoft']

For example companies[0:4] returns items from the list at index 0, 1, 2, 3 but not 4.

If you omit the start and end index, then python will return all the elements from the list.

In [18]: companies[:]
Out[18]: ['Google', 'Apple', 'Facebook', 'Microsoft', 'Netflix']

Or you can also provide one but not the other.

In [19]: companies[:3]
Out[19]: ['Google', 'Apple', 'Facebook']

In [20]: companies[2:]
Out[20]: ['Facebook', 'Microsoft', 'Netflix']

Modifying Elements of the List –

The syntax for modifying an element is similar to the syntax for accessing an element in a list. To change an element, use the name of the list followed by the index of the element you want to change and then provide the new value you want that item to have.

Let’s say you want to change the first item in the companies list.


In [21]: companies[0] = 'Airbnb'

In [22]: companies
Out[22]: ['Airbnb', 'Apple', 'Facebook', 'Microsoft', 'Netflix']

The above code changes the first item of the list from Google to Airbnb and the rest of the list stay same.

Adding Elements to a List –

You may want to add new items to a list. There are various ways of doing that, let’s look at them one by one.

Appending Elements to the End of the List –

The simplest way to add a new element to the list is to append it at the end of the list.

Let’s say you want to add the Google again to the list.

In [23]: companies.append('Google')

In [24]: companies
Out[24]: ['Airbnb', 'Apple', 'Facebook', 'Microsoft', 'Netflix', 'Google']

The append method adds the Google at the end of the list.

The append method makes it easy to build lists dynamically. For example you can start with an empty list then add items to the list using a series of append calls.

Let’s say you want to keep track of the name of the students in a class. You can start with an empty list then add the names to this list as new students join the class.


In [28]: students = []

In [29]: students.append('Nick')

In [30]: students.append('Jessica')

In [31]: students.append('Cece')

In [32]: students.append('Schmidt')

In [33]: students.append('Winston')

In [34]: students
Out[34]: ['Nick', 'Jessica', 'Cece', 'Schmidt', 'Winston']

Inserting Elements into a List –

You can also insert an item at a given position in the list using the insert() method. The syntax of insert() method is

list.insert(index, new_item)
In [35]: students.insert(4, 'Coach')

In [36]: students
Out[36]: ['Nick', 'Jessica', 'Cece', 'Schmidt', 'Coach', 'Winston']

Remove Elements from the List –

Sometimes you may also want to remove an item from the list. You can remove an item according to its position in the list or according to its value.

Removing an Item using the del statement –

If you know the position of the item you want to remove from the list, you can use the del statement.

Let’s say you want to remove Coach from the list.

In [37]: del students[4]

In [38]: students
Out[38]: ['Nick', 'Jessica', 'Cece', 'Schmidt', 'Winston']

Once you delete an item from the list using the del statement, it is gone forever. You can’t access it.

Removing an item using the pop() method –

Sometimes you may want to use the item that is removed from the list, in that case you can use the pop() method.

The pop() method removes the last item in the list and return it.

In [39]: popped_student = students.pop()

In [40]: popped_student
Out[40]: 'Winston'

In [41]: students
Out[41]: ['Nick', 'Jessica', 'Cece', 'Schmidt']

Removing Items from any Position in the List –

You can use pop() method to remove an item from any position in the list by including the index of the item you want to remove in parentheses.

Let’s remove Cece from the list.

In [42]: popped_student = students.pop(2)

In [43]: popped_student
Out[43]: 'Cece'

Removing an Item from the List by Value –

Sometimes you don’t know the position of the item that you want to remove from the list. But if you know the value of the item that you want to remove from the list, you can use the remove() method.

Let’s remove Schmidt from the list.

In [45]: students.remove('Schmidt')

In [46]: students
Out[46]: ['Nick', 'Jessica']

Let’s add the removed students to the list as we are going to use that list.

In [47]: students.append('Schmidt')

In [48]: students.append('Winston')

In [49]: students.append('Cece')

In [50]: students
Out[50]: ['Nick', 'Jessica', 'Schmidt', 'Winston', 'Cece']

Sorting a List temporarily with the sorted() function –

Sometimes you may want to sort the list. To sort a list temporarily you can use the sorted() function in python.

In [51]: # temporarily sort a list

In [52]: sorted(students)
Out[52]: ['Cece', 'Jessica', 'Nick', 'Schmidt', 'Winston']

In [53]: # original list

In [54]: students
Out[54]: ['Nick', 'Jessica', 'Schmidt', 'Winston', 'Cece']

To sort a list in reverse order, set the reverse parameter to True.

In [55]: # sort a list in reverse order

In [56]: sorted(students, reverse=True)
Out[56]: ['Winston', 'Schmidt', 'Nick', 'Jessica', 'Cece']

Sorting a List Permanently with sort() method –

To sort a List permanently you can use the the sort() method in python.

In [57]: # sort a list permanently 

In [58]: students.sort()

In [59]: students
Out[59]: ['Cece', 'Jessica', 'Nick', 'Schmidt', 'Winston']

To sort a list permanently in reverse order, set the reverse parameter to True.

In [60]: # sort a list permanently in reverse order

In [61]: students.sort(reverse=True)

In [62]: students
Out[62]: ['Winston', 'Schmidt', 'Nick', 'Jessica', 'Cece']

Print a List in Reverse Order –

To reverse the original order of a list, you can use the reverse() method.

In [64]: students.reverse()

In [65]: students
Out[65]: ['Cece', 'Jessica', 'Nick', 'Schmidt', 'Winston']

Find the Length of the List –

To find the length of the list in python, we use the len() function.

In [66]: # find the length of the list

In [67]: len(students)
Out[67]: 5

The student list has 5 items in it, so it’s length is 5.

Looping through a List in Python –

To loop through all the elements of a list in python, you can use a for loop.

Let’s say that we want to print all the names of students in the students list.

In [68]: for student in students:
    ...:     print(student)
    ...:     
Cece
Jessica
Nick
Schmidt
Winston

In [69]: 

Rating: 1 out of 5.

Leave a Reply