Lists in Python

In this post, we will learn about lists in python and how to use them in detail.

We got an overview of lists in python in the Python Basics tutorial’s part 8 (sequences). Make sure you check that out first if you haven’t already.

Lists in Python
Lists in Python

Lists can be thought of as dynamic sized arrays (like Java’s ‘ArrayList’ or C++’s ‘Vector’). They are mutable which means that they can be changed even after their creation.

We can also store elements of more that one data type in the same list. This means that a single can contain both integers and strings.

Creating a List

To declare a list in python, we just use square brackets and put the sequence inside them.

Let’s have a look using some code examples below.

>>> l = [1,2,3,'apple',5,6]
>>> print(l)
[1, 2, 3, 'apple', 5, 6]
>>> l = [1,2,3,4,5,6,7,8,9]
>>> print(l)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Length of a List

We can find the length of a list using the len() method. This will return the number of elements stored in the list.

>>> l = [1, 2, 3, 'apple', 5, 6]
>>> len(l)
6
>>> l = [1,2,3,4,5,6,7,8,9]
>>> len(l)
9

Adding elements to a List

We can add elements to a list using various methods.


Using append()

Using the append() method, we can add a single element at the end of the list.

>>> l.append(10)
>>> l.append(20)
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20]

Using insert()

We can add a single element at a specific position in a list using the insert() method as shown below.

>>> # Inserting the integer 11 at the 10th index.
>>> l.insert(10,11)
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 20]

>>> # Inserting the string 'twelve' at 11th index.
>>> l.insert(11,'twelve')
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'twelve', 20]

Using extend()

Using this extend() method, we can add multiple elements at the end of a list.

>>> l2 = [21,22,23,24]
>>> l.extend(l2)
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'twelve', 20, 21, 22, 23, 24]
>>>

Accessing elements of a List

List elements can be accessed using their index as shown below.

>>> l[0]
1
>>> l[2]
3

In python, we can also use negative indexing to access elements. When we use negative indexing, we can access elements from the end of the list. Let’s see using some examples below.

>>> l[-1]
24
>>> l[-2]
23
>>> l[-3]
22
>>>

Negative index ‘-1’ gives us the last element of the list. ‘-2’ will give us the second last element and this keeps going on.

>>> l[-len(l)]
1

Removing elements from a list

Using remove()

Remove() method removes the first occurrence of the searched element at a time. The element to be removed is passed as an argument to the method.

>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'twelve', 20, 21, 22, 23, 24]

# Removing the integer 11 from the list
>>> l.remove(11)
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'twelve', 20, 21, 22, 23, 24]

# Removing the string 'twelve' from the list
>>> l.remove('twelve')
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23, 24]

# Removing the integer 13 from the list (which does not exist)
>>> l.remove(13)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>>

As you can see above, if we try to remove an element which does not exist in the list, we get the following error:
ValueError: list.remove(x): x not in list“.

Using pop()

We can also remove elements from a list using the pop() method.

If we simply use the pop() method without any arguments, the last element of the list is removed.

>>> l.pop()
24
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23]
>>> l.pop()
23
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22]
>>>

We can also remove a specific element for a list using the pop() method. To do this, index of the element is passed as an argument to the pop() method.

>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22]

>>> l.pop(9)
10
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22]

>>> l.pop(9)
20
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 22]

One thought on “Lists in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: