4.1. Sequence list

  • Can store elements of any types

  • Mutable - can add, remove, and modify items

4.1.1. Type Definition

  • [] is used more often

  • list() is more readable

  • Comma after last element is optional

  • Brackets are required

Listing 38. Initialize empty
my_list = []
my_list = list()
Listing 39. Initialize with one element
my_list = [1]
my_list = [1,]
Listing 40. Initialize with many elements
my_list = [1, 2.0, None, False, 'Iris']
my_list = [1, 2.0, None, False, 'Iris',]
alphabet = list('ABCDE')
# ['A', 'B', 'C', 'D', 'E']

4.1.2. Type Annotation

my_list: list = list()
my_list: list = []
my_list: list = ['a', 1, 2.2]
from typing import List

my_list: List[int] = [1, 2, 3, 4]
my_list: List[float] = [5.8, 2.7, 5.1, 1.9]
my_list: List[str] = ['a', 'b', 'c', 'd']

4.1.3. Adding Elements

  • list + list

  • list.extend()

  • list.append()

my_list = [1, 2]

my_list + [3, 4]
# [1, 2, 3, 4]
Listing 41. Extending lists
my_list = [1, 2]

my_list.extend([3, 4])
# [1, 2, 3, 4]
Listing 42. Appending single item
my_list = [1, 2]

my_list.append(3)
# [1, 2, 3]
Listing 43. Appending multiple items
my_list = [1, 2]

my_list.append([3, 4])
# [1, 2, [3, 4]]

4.1.3.1. Inserting Elements at Specific Position

my_list = [1, 2]

my_list.insert(0, 'a')
# ['a', 1, 2]
my_list = [1, 2]

my_list.insert(1, 'a')
# [1, 'a', 2]

4.1.4. Modification

4.1.4.1. Getting Items

my_list = ['a', 'b', 'c', 'd']

my_list[0]         # 'a'
my_list[1]         # 'b'
my_list[2]         # 'c'
my_list[3]         # 'd'

4.1.4.2. Setting Items

my_list = ['a', 'b', 'c', 'd']

my_list[0] = 'x'

print(my_list)
# ['x', 'b', 'c', 'd']

4.1.4.3. Deleting Items

my_list = ['a', 'b', 'c', 'd']

del my_list[3]

print(my_list)
# ['a', 'b', 'c']
my_list = ['a', 'b', 'c', 'd']

my_list.pop()
# 'd'

print(my_list)
# ['a', 'b', 'c']

4.1.5. Sorting

Timsort is a hybrid sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was invented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsets of the data that are already ordered, and uses the subsets to sort the data more efficiently. This is done by merging an identified subset, called a run, with existing runs until certain criteria are fulfilled. Timsort has been Python's standard sorting algorithm since version 2.3. It is now also used to sort arrays in Java SE 7, and on the Android platform.

  • sorted() - Returns sorted list, do not modify the original

  • list.sort() - Changes object permanently, returns None

a = [3, 1, 2]
b = sorted(a)

print(a)
# [3, 1, 2]

print(b)
# [1, 2, 3]
a = [3, 1, 2]
b = a.sort()

print(a)
# [1, 2, 3]

print(b)
# None

4.1.6. Multiple Statements in One Line

my_list = [3, 1, 2]

my_list.sort().append(4)
# AttributeError: 'NoneType' object has no attribute 'append'
my_list = [3, 1, 2]
my_list.sort()
my_list.append(4)

print(my_list)
# [1, 2, 3, 4]

4.1.7. Membership Operators

  • == - Eq (equals)

  • != - Ne (not-equals)

  • in - Contains

  • not in - Missing

[1, 2] == [1, 2]        # True
[1, 2] == [2, 1]        # False

[1, 2, 3] != [1, 2]     # True
[1, 2] != [1, 2]        # False

1 in [1, 2]             # True
3 in [1, 2]             # False

[2] in [1, 2]           # False
[1, 2] in [1, 2]        # False

[2] in [1, [2]]         # False
[1, 2] in [[1,2], 3]    # True

4 not in [1, 2]         # True
1 not in [1, 2]         # False

[2] not in [1, 2]       # True
[1, 2] not in [1, 2]    # True

4.1.8. Built-in Functions on Sequences

  • min() - Minimal value

  • max() - Maximal value

  • sum() - Sum of elements

  • len() - Length of a list

min([1, 2, 3, 4, 5])
# 1

max([1, 2, 3, 4, 5])
# 5

sum([1, 2, 3, 4, 5])
# 15

len([1, 2, 3])
# 3

4.1.9. Assignments

4.1.9.1. Create

  • Complexity level: easy

  • Lines of code to write: 3 lines

  • Estimated time of completion: 5 min

  • Filename: solution/list_create.py

English
  1. For given data input (see below)

  2. Create a list representing each row

  3. To convert table use multiline select with alt key in your IDE

Polish
  1. Dla danych wejściowych (patrz sekcja input)

  2. Stwórz list reprezentujący każdy wiersz

  3. Do przekonwertowania tabelki wykorzystaj zaznaczanie wielu linijek za pomocą klawisza alt w Twoim IDE

Input
Table 7. Input data

Sepal length

Sepal width

Petal length

Petal width

Species

5.8

2.7

5.1

1.9

virginica

5.1

3.5

1.4

0.2

setosa

5.7

2.8

4.1

1.3

versicolor

6.3

2.9

5.6

1.8

virginica

6.4

3.2

4.5

1.5

versicolor

Output
a = [5.8, 2.7, 5.1, 1.9, 'virginica']
b = [5.1, 3.5, 1.4, 0.2, 'setosa']
c = [5.7, 2.8, 4.1, 1.3, 'versicolor']
d = [6.3, 2.9, 5.6, 1.8, 'virginica']
e = [6.4, 3.2, 4.5, 1.5, 'versicolor']
The whys and wherefores
  • Defining list

  • Learning IDE features