3) c. Lists

Data structures are a special ways of storing groups of data together for convenience and efficiency. They’re also referred to as Containers. All programming languages allow the use of data structures.

List is one of the most commonly used data structures in Python.

A list variable is fundamentally different from the types we looked before. A list is what is known as a data structure or a container. It is made to hold values inside. Think of it like a bag that holds various things. And list is just one of the many types of “bags” each of which works in its unique way.



You can use square brackets to create a list that can hold variables of different types. Also, as you probably already noticed, the order of the elements in a list starts with 0 not 1 which can be confusing to people who are new to programming. But even then, different languages have different ideas where to start counting the elements in containers.

Let’s look at its characteristics.

  • Lists are ordered sets (list elements keep their order)
  • Lists are mutable (they can be changed, i.e. elements added or removed)

You can initialize an empty list in two ways.

1
2
list1 = list()
list2 = []

The first method uses the list function and the second method uses a literal notation of an empty list.

You can also use the list function to convert an iterable object into a list.

1
2
Output:
['a', 'b', 'c', 'd', 'e', 'f', 'g']



Lists have the following methods:

append(e): Add an element at the end of the list

1
2
Output:
[1, 2, 3]

 




extend(iterable): Extend list with elements from iterable.

1
2
Output:
[1, 2, 3, 4, 5, 6]

 




insert(i, e): Insert an element (e) at position (i) of the list.

1
2
Output:
[1, 2, 3, 4]

 




remove(e): Remove first element with value e.

1
2
Output:
[1, 3, 2]

 




pop([i]): Remove and return element at position i. If i is not specified the last element will be removed and returned.

1
2
3
4
5
Output:
3
[1, 2]
1
[2]

 




clear(): Remove all elements.

1
2
Output:
[]

 




index(e, [, start[, end]]): Return position of element e. You can specify optional start and stop positions to limit the search.

1
2
Output:
The author name is located at  12

 




count(e): Count occurrences of e in the list.

1
2
Output:
3

 




sort(key=None, reverse=False): Sort the elements in place.

1
2
3
Output:
['Alex', 'Jack', 'Max', 'Michael', 'Sarah']
['Michael', 'Sarah', 'Alex', 'Jack', 'Max']

 




reverse(): Reverse the elements in place.

1
2
Output:
['Alex', 'Max', 'Sarah', 'Michael', 'Jack']

 




copy(): Returns a shallow copy of the list.

1
2
Output:
['apple', 'orange', 'banana']

 




Finally you can declare a list through a “comprehension“. A “List comprehension” simply means creating a new list by writing a single line of code that is more readable (aka easier to comprehend).

In the following example we have a list of numbers and you want to get a list of these numbers squared. We have used two approaches here. See if you can spot the differences between these two approaches.

1
2
3
4
5
6
7
8
9
my_numbers = [1, 2, 3, 4, 5]

# Approach 1. Simple but long.
squared_numbers = []
for number in my_numbers:
    squared_numbers.append(number * number)

# Approach 2. Using list comprehension. Short and sweet.
squared_numbers = [number * number for number in my_numbers]



Note here the meaning of the list comprehension. It uses a simple for-loop. We shall cover for-loop in extensive details in the next chapter. For now, you must know that for-loop basically takes a data structure, then picks up every element within it.

In our example here, for-loop goes through structure my_numbers and then assigns each value to number. So when we say number * number, we are essentially squaring every element in the list and a new list gets created.

Don’t worry if this concept feels very new right now. We shall be picking up plenty of examples on list comprehensions going forward and you will have lots of practice!




Here is an image that shows you a summary of the most commonly-used functions for Python lists.