3.1. Sequence List

3.1.1. Rationale

  • Can store elements of any types

  • Mutable - can add, remove, and modify items

3.1.2. Type Definition

  • [] is used more often

  • list() is more readable

  • Comma after last element is optional

  • Brackets are required

data = list()
data = []

data = [1]
data = [1, 2, 3]
data = [1.1, 2.2, 3.3]
data = [True, False]
data = ['a', 'b', 'c']
data = ['a', 1, 2.2, True, None]
list('hello')
# ['h', 'e', 'l', 'l', 'o']

list('ABCD')
# ['A', 'B', 'C', 'D']

3.1.3. Type Casting

  • list() converts argument to list

data = [1, 2, 3]
list(data)
# [1, 2, 3]
data = (1, 2, 3)
list(data)
# [1, 2, 3]
data = {1, 2, 3}
list(data)
# [1, 2, 3]
data = frozenset({1, 2, 3})
list(data)
# [1, 2, 3]

3.1.4. Get Item

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

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

3.1.5. Set Item

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

data[0] = 'x'
print(data)
# ['x', 'b', 'c', 'd']

data[4] = 'x'
# IndexError: list assignment index out of range

3.1.6. Del Item

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

del data[3]

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

value = data.pop()

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

print(value)
# 'd'

3.1.7. Append

  • list + list

  • list.extend()

  • list.append()

Listing 3.1. Adding two lists
data = [1, 2]

data + [3, 4]
# [1, 2, 3, 4]
Listing 3.2. Extending lists
data = [1, 2]

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

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

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

3.1.8. Insert

  • Insert at specific position

data = [1, 2]

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

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

3.1.9. Sort

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

3.1.10. Method Chaining

data = [3, 1, 2]

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

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

3.1.11. Built-in Functions

  • min() - Minimal value

  • max() - Maximal value

  • sum() - Sum of elements

  • len() - Length of a list

  • all() - All values are True

  • any() - Any values is True

data = [2, 0, 1]

min(data)       # 0
max(data)       # 2
sum(data)       # 3
len(data)       # 3
any(data)       # True
all(data)       # False
data = [True, False, True]

min(data)       # False
max(data)       # True
sum(data)       # 2
len(data)       # 3
any(data)       # True
all(data)       # False

3.1.12. Assignments

3.1.12.1. List Create

English
  1. Create list result with elements:

    • 'a'

    • 1

    • 2.2

  2. Print result

  3. Print number of elements in result

Polish
  1. Stwórz listę result z elementami:

    • 'a'

    • 1

    • 2.2

  2. Wypisz result

  3. Wypisz liczbę elementów result

3.1.12.2. List Many

English
  1. Use data from "Input" section (see below)

  2. Create lists a, b, c representing each row

  3. a with data from row 1

  4. b with data from row 2

  5. c with data from row 3

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Stwórz listy a, b, c reprezentujące każdy wierszy

  3. a z danymi z wiersza 1

  4. b z danymi z wiersza 2

  5. c z danymi z wiersza 3

Input
Table 3.1. Input data

Row

Sepal length

Sepal width

Petal length

Petal width

Species

1

5.8

2.7

5.1

1.9

virginica

2

5.1

3.5

1.4

0.2

setosa

3

5.7

2.8

4.1

1.3

versicolor

The whys and wherefores
  • Defining list

  • Learning IDE features

3.1.12.3. Modify

English
  1. Use data from "Input" section (see below)

  2. Insert at the begin of a last element popped from b

  3. Append to the b last element popped from a

  4. For getting elements use list.pop()

  5. From list c using del delete last element

  6. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Na początek a wstaw ostatni element wyciągnięty z b

  3. Na koniec b wstaw ostatni element wyciągnięty z a

  4. Do wyciągnięcia używaj list.pop()

  5. Z listy c za pomocą del usuń last element

  6. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
a = [4.7, 3.2, 1.3, 0.2, 'setosa']
b = [7.0, 3.2, 4.7, 1.4, 'versicolor']
c = [7.6, 3.0, 6.6, 2.1, 'virginica']
Output
a = ['versicolor', 4.7, 3.2, 1.3, 0.2]
b = [7.0, 3.2, 4.7, 1.4, 'setosa']
c = [7.6, 3.0, 6.6, 2.1]
The whys and wherefores
  • Defining list

  • Learning IDE features