3.6. Sequence Get Item

3.6.1. Rationale

  • Index must int (positive, negative or zero)

  • Index must be less or equal to length of object

  • Negative index starts from the end and go right to left

3.6.2. Positive Index

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

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

3.6.3. Negative Index

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

data[-0]            # 'a'
data[-1]            # 'e'
data[-2]            # 'd'

3.6.4. Out of Range

Listing 3.19. Accessing not existing element
data = ['a', 'b', 'c', 'd', 'e']

data[100]           # IndexError: string index out of range
data[-100]          # IndexError: string index out of range

3.6.5. Ordered Sequence

Listing 3.20. Get item from str
data = 'abcde'

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

data[-0]            # 'a'
data[-1]            # 'e'
data[-2]            # 'd'
Listing 3.21. Get item from list
data = ['a', 'b', 'c', 'd', 'e']

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

data[-0]            # 'a'
data[-1]            # 'e'
data[-2]            # 'd'
Listing 3.22. Get item from tuple
data = ('a', 'b', 'c', 'd', 'e')

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

data[-0]            # 'a'
data[-1]            # 'e'
data[-2]            # 'd'

3.6.6. Unordered Sequence

Listing 3.23. Get item from set is impossible. set is unordered data structure.
data = {'a', 'b', 'c', 'd', 'e'}

data[0]             # TypeError: 'set' object is not subscriptable
data[1]             # TypeError: 'set' object is not subscriptable
data[2]             # TypeError: 'set' object is not subscriptable

data[-0]            # TypeError: 'set' object is not subscriptable
data[-1]            # TypeError: 'set' object is not subscriptable
data[-2]            # TypeError: 'set' object is not subscriptable
Listing 3.24. Get item from frozenset is impossible. frozenset is unordered data structure.
data = {'a', 'b', 'c', 'd', 'e'}

data[0]             # TypeError: 'frozenset' object is not subscriptable
data[1]             # TypeError: 'frozenset' object is not subscriptable
data[2]             # TypeError: 'frozenset' object is not subscriptable

data[-0]            # TypeError: 'frozenset' object is not subscriptable
data[-1]            # TypeError: 'frozenset' object is not subscriptable
data[-2]            # TypeError: 'frozenset' object is not subscriptable

3.6.7. Assignments

3.6.7.1. Get Item Select

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

  2. Write header (first line) to header: tuple variable

  3. Create result: list

  4. Convert to list data from row at index 2, 6, 9 and add to result

  5. Convert to tuple data from row at index 12, 15, 16 and add to result

  6. Add empty set, list and tuple to result

  7. Use only indexes

  8. Do not use for, while or slice()

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

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

  2. Zapisz nagłówek (pierwsza linia) do zmiennej header: tuple

  3. Stwórz result: list

  4. Przekonwertuj do list dane z wierszy o indeksach 2, 6, 9 i dodaj do result

  5. Przekonwertuj do tuple dane z wierszy o indeksach 12, 15, 16 i dodaj do result

  6. Dodaj pusty set, list oraz tuple do result

  7. Użyj tylko indeksów

  8. Nie używaj for, while lub slice()

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

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'),
    (4.7, 3.2, 1.3, 0.2, 'setosa'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
    (4.9, 3.0, 1.4, 0.2, 'setosa'),
    (4.9, 2.5, 4.5, 1.7, 'virginica'),
    (7.1, 3.0, 5.9, 2.1, 'virginica'),
    (4.6, 3.4, 1.4, 0.3, 'setosa'),
    (5.4, 3.9, 1.7, 0.4, 'setosa'),
    (5.7, 2.8, 4.5, 1.3, 'versicolor'),
    (5.0, 3.6, 1.4, 0.3, 'setosa'),
    (5.5, 2.3, 4.0, 1.3, 'versicolor'),
    (6.5, 3.0, 5.8, 2.2, 'virginica'),
    (6.5, 2.8, 4.6, 1.5, 'versicolor'),
    (6.3, 3.3, 6.0, 2.5, 'virginica'),
    (6.9, 3.1, 4.9, 1.5, 'versicolor'),
    (4.6, 3.1, 1.5, 0.2, 'setosa'),
]
Output
header: tuple
# ('Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species')

data: list
# [[5.1, 3.5, 1.4, 0.2, 'setosa'],
#  [4.7, 3.2, 1.3, 0.2, 'setosa'],
#  [4.9, 3.0, 1.4, 0.2, 'setosa'],
#  (4.6, 3.4, 1.4, 0.3, 'setosa'),
#  (5.0, 3.6, 1.4, 0.3, 'setosa'),
#  (5.5, 2.3, 4.0, 1.3, 'versicolor'),
#  set(),
#  [],
#  ()]
The whys and wherefores
  • Using nested data structures

  • Using indexes

  • Type casting