Lists

1. The list is a type of data in Python used to store multiple objects. It is an ordered and mutable collection of comma-separated items between square brackets, e.g.:

2. Lists can be indexed and updated, e.g.:


3. Lists can be nested, e.g.:


4. List elements and lists can be deleted, e.g.:


5. Lists can be iterated through using the for loop, e.g.:


6. The len() function may be used to check the list’s length, e.g.:


7. A typical function invocation looks as follows: result = function(arg), while a typical method invocation looks like this:result = data.method(arg).

8. You can use the sort() method to sort elements of a list, e.g.:

9. There is also a list method called reverse(), which you can use to reverse the list, e.g.:

10. If you have a list list1, then the following assignment: list2 = list1 does not make a copy of the list1 list, but makes the variables list1 and list2 point to one and the same list in memory. For example:

11. If you want to copy a list or part of the list, you can do it by performing slicing:


12. You can use negative indices to perform slices, too. For example:


13. The start and end parameters are optional when performing a slice: list[start:end], e.g.:


14. You can delete slices using the del instruction:


15. You can test if some items exist in a list or not using the keywords in and not in, e.g.:

16. List comprehension allows you to create new lists from existing ones in a concise and elegant way. The syntax of a list comprehension looks as follows:


which is actually an equivalent of the following code:

Let’s assume that we’re able to use the selected numbers to represent any chess piece. We can also assume that every row on the chessboard is a list.

Look at the code below:

It builds a list containing eight elements representing the second row of the chessboard – the one filled with pawns (assume that WHITE_PAWN is a predefined symbol representing a white pawn).

The same effect may be achieved by means of a list comprehension, the special syntax used by Python in order to fill massive lists.

The part of the code placed inside the brackets specifies:

the data to be used to fill the list (WHITE_PAWN)
the clause specifying how many times the data occurs inside the list (for i in range(8))

Here’s an example of a list comprehension ‒ the code creates a five-element list filled with the first five natural numbers raised to the power of 3:


17. Swapping the lists’s elements.

you can easily swap the list’s elements to reverse their order:


Run the snippet. Its output should look like this:

[5, 3, 8, 1, 10]

Will it still be acceptable with a list containing 100 elements? No, it won’t. You use the for loop to do the same thing automatically, irrespective of the list’s length.


This is how we’ve done it:

 

 

Let us show you some other list comprehension examples:

Example #1:


The snippet produces a ten-element list filled with squares of ten integer numbers starting from zero (0, 1, 4, 9, 16, 25, 36, 49, 64, 81)

Example #2:

The snippet creates an eight-element array containing the first eight powers of two (1, 2, 4, 8, 16, 32, 64, 128)

Example #3:


The snippet makes a list with only the odd elements of the squares list.

18. You can use nested lists in Python to create matrices (i.e., two-dimensional lists). For example:

Table - a two-dimensional array


 

18. You can nest as many lists in lists as you want, thereby creating n-dimensional lists, e.g., three-, four- or even sixty-four-dimensional arrays. For example:

Cube - a three-dimensional array


 

Exercise 1

What is the output of the following snippet?

 

Exercise 2

What is the output of the following snippet?

 

Exercise 3

What happens when you run the following snippet?

Exercise 4

What is the output of the following snippet?

 

Exercise 5

What is the output of the following snippet?

 

Exercise 6

What is the output of the following snippet?

 

Exercise 7

What is the output of the following snippet?

 

Exercise 8

What is the output of the following snippet?

 

Exercise 9

Insert in or not in instead of ??? so that the code outputs the expected result.

 

Exercise 10

What is the output of the following snippet?

lst = ["D", "F", "A", "Z"]
lst.sort()

print(lst)

 

['A', 'D', 'F', 'Z']

 

Exercise 11

What is the output of the following snippet?

a = 3
b = 1
c = 2

lst = [a, c, b]
lst.sort()

print(lst)

 

[1, 2, 3]

 

Exercise 12

What is the output of the following snippet?

a = "A"
b = "B"
c = "C"
d = " "

lst = [a, b, c, d]
lst.reverse()

print(lst)

 

[' ', 'C', 'B', 'A']

 

Scenario

Imagine a list – not very long, not very complicated, just a simple list containing some integer numbers. Some of these numbers may be repeated, and this is the clue. We don’t want any repetitions. We want them to be removed.

Your task is to write a program which removes all the number repetitions from the list. The goal is to have a list in which all the numbers appear not more than once.

Note: assume that the source list is hard-coded inside the code – you don’t have to enter it from the keyboard. Of course, you can improve the code and add a part that can carry out a conversation with the user and obtain all the data from her/him.

Hint: we encourage you to create a new list as a temporary work area – you don’t need to update the list in situ.