Lesson 11: Lists & Exercises
Lists are used to store an ordered and changeable collection of items inside a single variable.
Principle:
A list can be imagined like a single-row table divided into several cells. Every cell has its own index, and indexes are counted from zero.
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Value | 10 | 15 | 8 | 11 |
List's element 0 is 10, list's element 1 is 15, list's element 2 is 8, and list's element 3 is 11.
In Python, we can also use negative indexes. These are counted from the back:
| Negative Index | -4 | -3 | -2 | -1 |
|---|---|---|---|---|
| Value | 10 | 15 | 8 | 11 |
list[-1] = 11list[-2] = 8list[-3] = 15list[-4] = 10
Lists are created using square brackets, for example: [7, 6, -90, 107, -18] or ["Berlin", "Prague", "Vienna", "Warsaw"].
Components of a list don’t have to be the same data type: ["Pizza", 2, 4.8, [10, 20]].
len(list) returns the number of elements in the list.
3 Ways of Creating a List:
1. Putting elements directly in the code:
NameOfList = [element1, element2, ...]
# Example:
test = [10, 8, 15]
2. Adding elements during the run of the program:
First, we create an empty list: test = []
Then we add elements using insert or append:
- Insert:
test.insert(index, element)→ e.g.,test.insert(1, int(input("Enter a number: ")))
We can use this inside a loop:
test = []
for i in range(3):
test.insert(i, int(input("Enter a number: ")))
print(test)Output
Enter a number: 8
Enter a number: 22
Enter a number: -90
[8, 22, -90]
- Append:
test.append(element)→ The element will be added to the very end of the list.
3. Using the list() function:
An empty list will be created without arguments, or converted from other objects:
years = list(range(2006, 2009)) # → [2006, 2007, 2008]
car = list("mercedes") # → ['m', 'e', 'r', 'c', 'e', 'd', 'e', 's']
Deleting Elements from a List:
- If we know the value of an element we want to delete:
list.remove(value) - If we know the index of the element we want to delete:
list.pop(index)
Accessing List Elements (Slicing):
list[m]– takes the element at indexm.list[m:n]– takes elements in range from indexmton-1.list[m:n:i]– takes elements in range frommton-1with a step ofi.
If m is not specified, it starts from the beginning of the list (0). If n is not specified, it ends at the end of the list.
Going Through a List:
- For each function: By this way, we can't edit the elements, but we can print them:
for element in list: print(element) - Using the
range()function: By this way, we access elements with an index and we can even edit them:for i in range(len(list)): list[i] = list[i] + 1 # all elements are incremented by one
Operations with Lists:
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c) # → [1, 2, 3, 4, 5, 6]
# Multiplication:
[1, 2] * 4 # → [1, 2, 1, 2, 1, 2, 1, 2]
List Exercises:
1st Exercise:
You have a list of integers named numbers. Write a Python script using a loop that creates a new list named evens containing only the even numbers from the original list, maintaining their original order.
Click the code block below to copy it:
Reveal Answer
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
evens = []
for a in numbers:
if a % 2 == 0:
evens.append(a)
print(evens)Output
[2, 4, 6, 8]2nd Exercise:
You have a list of integers named numbers that contains some zeros. Write a Python script that moves all the zeros to the end of the list while keeping the original relative order of the non-zero elements. Do not use built-in sorting methods.
Click the code block below to copy it:
Reveal Answer
numbers = [0, 1, 0, 3, 12]
noZero = []
zeroCount = 0
for a in numbers:
if a != 0:
noZero.append(a)
else:
zeroCount += 1
for i in range(zeroCount):
noZero.append(0)
numbers = noZero
print(numbers)Output
[1, 3, 12, 0, 0]Want XP, levels and progress tracking? Play this lesson in the CodeStart game.
Play Lesson 11