A list is a collection of objects

Everything in Python is an object. These objects in a list are numbers in most cases. Lists can also contain text (strings) or both.

Note: Lists can have zero or more elements (empty lists are possible).

Related Course: Complete Python Programming Course & Exercises

Python Lists

A list is a collection of objects. This is something like an array in other languages. A list is enclosed by brackets [] and each object is separated by a comma, as shown below:

>>> a = ['meow','bar','woof']

>>> a
['meow', 'bark', 'woof']
>>> print(a)
['meow', 'bark', 'woof']
>>>

Important features of a list are:

  • Lists are ordered
  • List can contain arbitrary objects
  • List element can be accessed by index
  • List are mutable
  • Lists are dynamic

Lists are ordered

A list is an ordered collection of objects. The order in which you add elements to your list is stored.

>>> a = ['meow','bark','woof']
>>> print(a)
['meow', 'bark', 'woof']

If you compare two lists (==) and the order is not the same, then the lists are not equal.

>>> [1,2,3] == [3,2,1]
False
>>> ['meow','bark','woof'] == ['meow','woof','bark']
False

Only if the list contents is exactly the same, the lists are equal.

>>> a = ['meow','bark','woof']
>>> b = ['meow','bark','woof']
>>> a == b
True

python list

Related Course: Complete Python Programming Course & Exercises

List can contain arbitrary objects

A list can contain any type ofobject. A list can contain numeric elements:

>>> a = [1,3,6,9]

It can be strings:

>>> a = ["meow","bark","woof"]

A combination of types if fine too:

>>> a = [1,"meow",2,3,"bark"]

A list can have any number of elements. It can be empty [] or have many objects.

>>> a = []
>>> a
[]

>>> a = [1]
>>> a
[1]

>>> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

The items in a list don’t have to be unique.

>>> a = ['meow','bark','meow','woof']

List elements can be accessed by index

Individual elements in the list can be accesses by an index. The index starts counting from zero, where [0] is the first element in the list.

If this is your list:

>>> a = ['meow','woof','bark','cheep']

Then you can access individual elements using the index:

>>> a[0]
'meow'
>>> a[1]
'woof'
>>> a[2]
'bark'
>>> a[3]
'cheep'
>>>

python access list items

To print the last element, you can count from the back using the minus sign.

print(a[-1])
Result

cheep

You can do slicing on lists, the same way as you would slice strings.
The example below slices the list twice:

>>> a = ['A','B','C','D','E','F']

>>> a[0:2]
['A', 'B']

>>> a[3:5]
['D', 'E']
>>>

If first index is zero, it can be omitted:

>>> a[:2]
['A', 'B']

You can check if a list contains an element, by using the in keyword:

>>> a = ['meow','woof','bark','cheep']
>>> 'meow' in a
True
>>> 'grr' in a
False
>>>

Lists are mutable

A list is a mutable data type. After creation of a list, the list can be changed. Elements can be added, modified, deleted or moved around.

>>> a = ['A','B']
>>> a.append('C')
>>> a
['A', 'B', 'C']

>>> del a[0]
>>> a
['B', 'C']

>>> a[0] = 'A'
>>> a
['A', 'C']
>>>

Lists are dynamic

A list is dynamic. They can grow or shrink in size during the execution of your program.

You can grow a list like this:

>>> a = []
>>> a += [1]
>>> a += [2]
>>> a += [3]
>>> a
[1, 2, 3]
>>>

Or using the append() function:

>>> a = []
>>> a.append(1)
>>> a.append(2)
>>> a.append(3)
>>> a
[1, 2, 3]
>>>

The list can shrink, if elements are deleted:

>>> del a[2]

Python list get size

You can get the length of the list using the len() function. This function can be applied to any list. It will return the number of elements in the list.

Example code:

>>> a = []
>>> len(a)
0
>>> a = ['meow','woof','bark','cheep']
>>> len(a)
4

Python list remove

By calling the remove() method, you can remove an element. This method is called on the list object.

>>> a = ['meow','woof','bark','cheep']
>>> a.remove('woof')
>>> a
['meow', 'bark', 'cheep']

Python list append in front

The method append() adds an element to the end of the list. What if you want to append to the front?

You can use array.insert(index, value), like so:

>>> a = ['meow','woof','bark','cheep']
>>> a.insert(0,'bzzz')
>>> a
['bzzz', 'meow', 'woof', 'bark', 'cheep']
>>>

You can also do this:

>>> a = ['meow','woof','bark','cheep']
>>> a = ['bzzz'] + a
>>> a
['bzzz', 'meow', 'woof', 'bark', 'cheep']
>>>

Python list pop

The pop() method removes the last object from the list. The method returns the removed object.

>>> x = [1,2,3,4,5]
>>> x.pop()
5
>>> x
[1, 2, 3, 4]
>>>

Python list sort

You can sort a list by calling the .sort() method. Call this method on the list itself. The method sorts from low to high.

>>> a = [6,3,4,2,5,1]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5, 6]
>>>

If you want in reverse order, just add the parameter reverse=True.

>>> a = [6,3,4,2,5,1]
>>> a.sort(reverse=True)
>>> a
[6, 5, 4, 3, 2, 1]
>>>

Python list count

If you want to know how many times an object occurs inside a list, you can use the count() method.

The example below counts the frequency of some items:

>>> a = [6,5,4,1,2,4,3]
>>> a.count(4)
2
>>> a.count(3)
1
>>>

This works for any type of data in the list:

>>> a = ['meow','foo','meow','meow']
>>> a.count('meow')
3
>>>

If you are a Python beginner, then I highly recommend this book.

Download exercises