8.2. Unpack Slice
8.2.1. Summary
Slice argument must be
int
(positive, negative or zero)Positive Index starts with
0
Negative index starts with
-1
8.2.2. Slice Forwards
sequence[start:stop]
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> data[0:3]
['a', 'b', 'c']
>>>
>>> data[3:5]
['d', 'e']
8.2.3. Slice Defaults
sequence[start:stop]
start
defaults to0
stop
defaults tolen(sequence)
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>> data[0:3]
['a', 'b', 'c']
>>>
>>> data[:3]
['a', 'b', 'c']
>>> data[3:5]
['d', 'e']
>>>
>>> data[3:]
['d', 'e']
>>>
>>> data[3:len(data)]
['d', 'e']
>>> data[:]
['a', 'b', 'c', 'd', 'e']
8.2.4. Slice Backwards
Negative index starts from the end and go right to left
>>> data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
... # 0 1 2 3 4 5 6 7 8 9
... # -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
>>>
>>> data[-3:-1]
['h', 'i']
>>>
>>> data[-3:]
['h', 'i', 'j']
>>>
>>> data[0:-3]
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>>
>>> data[:-3]
['a', 'b', 'c', 'd', 'e', 'f', 'g']
8.2.5. Step Forward
Every
n
-th elementsequence[start:stop:step]
start
defaults to0
stop
defaults tolen(sequence)
step
defaults to1
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>> data[0:5:1]
['a', 'b', 'c', 'd', 'e']
>>> data[0:5:2]
['a', 'c', 'e']
>>>
>>> data[1:5:2]
['b', 'd']
>>> data[:5:2]
['a', 'c', 'e']
>>>
>>> data[1::2]
['b', 'd']
>>> data[::2]
['a', 'c', 'e']
8.2.6. Step Backward
Every
n
-th elementsequence[start:stop:step]
start
defaults to0
stop
defaults tolen(sequence)
step
defaults to1
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> data[5:0:-1]
['e', 'd', 'c', 'b']
>>>
>>> data[5::-1]
['e', 'd', 'c', 'b', 'a']
>>>
>>> data[:0:-1]
['e', 'd', 'c', 'b']
>>>
>>> data[5::-2]
['e', 'c', 'a']
>>>
>>> data[:0:-2]
['e', 'c']
>>>
>>> data[::-1]
['e', 'd', 'c', 'b', 'a']
>>>
>>> data[::-2]
['e', 'c', 'a']
8.2.7. Out of Range
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> data[0:100]
['a', 'b', 'c', 'd', 'e']
8.2.8. Index Arithmetic
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> start = 1
>>> stop = 5
>>> step = 1
>>>
>>> data[start:stop:step]
['b', 'c', 'd', 'e']
>>>
>>> data[start-1:stop-2:step]
['a', 'b', 'c']
8.2.9. Slice Errors
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> data['a':'e']
Traceback (most recent call last):
TypeError: slice indices must be integers or None or have an __index__ method
>>>
>>> data[::0]
Traceback (most recent call last):
ValueError: slice step cannot be zero
>>>
>>> data[:10.0]
Traceback (most recent call last):
TypeError: slice indices must be integers or None or have an __index__ method
8.2.10. Slice str
>>> data = 'abcde'
>>>
>>> data[0:3]
'abc'
>>>
>>> data[-3:]
'cde'
>>>
>>> data[::2]
'ace'
8.2.11. Slice tuple
>>> data = ('a', 'b', 'c', 'd', 'e')
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> data[0:3]
('a', 'b', 'c')
>>>
>>> data[-3:]
('c', 'd', 'e')
>>>
>>> data[::2]
('a', 'c', 'e')
8.2.12. Slice list
>>> data = ['a', 'b', 'c', 'd', 'e']
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> data[0:3]
['a', 'b', 'c']
>>>
>>> data[-3:]
['c', 'd', 'e']
>>>
>>> data[::2]
['a', 'c', 'e']
8.2.13. Slice set
Slicing set
is not possible:
>>> data = {'a', 'b', 'c', 'd', 'e'}
... # 0 1 2 3 4
... # -5 -4 -3 -2 -1
>>>
>>> data[0:3]
Traceback (most recent call last):
TypeError: 'set' object is not subscriptable
8.2.14. Slice dict
Slicing on
dict
is not possible
>>> data = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> data[0:3]
Traceback (most recent call last):
KeyError: slice(0, 3, None)
>>>
>>> data['a':'d']
Traceback (most recent call last):
KeyError: slice('a', 'd', None)
8.2.15. Nested Sequences
>>> data = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]
... ]
>>>
>>> data[:2]
[[1, 2, 3], [4, 5, 6]]
>>>
>>> data[:2][1:]
[[4, 5, 6]]
>>>
>>> data[:2][1:][0]
[4, 5, 6]
>>>
>>> data[:2][1:][0][1:]
[5, 6]
>>>
>>> data[:2][1:][0][1:][1]
6
8.2.16. Column Selection
Column selection unfortunately does not work on list
:
>>> data = [[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]]
...
>>> data[:]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>
>>> data[:][1]
[4, 5, 6]
>>>
>>> data[:, 1]
Traceback (most recent call last):
TypeError: list indices must be integers or slices, not tuple
However this syntax is valid in numpy
and pandas
.
8.2.17. Use Case - 1
>>> from pprint import pprint
>>>
>>>
>>> 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'),
... ]
>>>
>>>
>>> pprint(DATA[1:])
[(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')]
>>>
>>> pprint(DATA[1::2])
[(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.4, 3.2, 4.5, 1.5, 'versicolor')]
>>>
>>> pprint(DATA[1::-2])
[(5.8, 2.7, 5.1, 1.9, 'virginica')]
>>>
>>> pprint(DATA[:1:-2])
[(4.7, 3.2, 1.3, 0.2, 'setosa'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa')]
>>>
>>> pprint(DATA[:-5:-2])
[(4.7, 3.2, 1.3, 0.2, 'setosa'), (6.3, 2.9, 5.6, 1.8, 'virginica')]
>>>
>>> pprint(DATA[1:-5:-2])
[]
8.2.18. Use Case - 2
>>> data = [[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]]
...
>>> data[::2]
[[1, 2, 3],
[7, 8, 9]]
>>>
>>> data[::2][1]
[7, 8, 9]
>>>
>>> data[::2][:1]
[[1, 2, 3]]
>>>
>>> data[::2][1][1:]
[8, 9]
8.2.19. Use Case - 3
>>> text = 'We choose to go to the Moon!'
>>> word = 'Moon'
>>>
>>>
>>> start = text.find(word)
>>> stop = start + len(word)
>>>
>>> text[start:stop]
'Moon'
>>>
>>> text[:start]
'We choose to go to the '
>>>
>>> text[stop:]
'!'
>>>
>>> text[:start] + text[stop:]
'We choose to go to the !'
8.2.20. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Unpack Slice Text
# - Difficulty: easy
# - Lines: 6
# - Minutes: 8
# %% English
# 1. From each `DATA_*` variable remove scientific title, military rank, etc.
# 2. Define `result_a: str` with text 'Mark Watney' sliced from `DATA_A`
# 3. Define `result_b: str` with text 'Melissa Lewis' sliced from `DATA_B`
# 4. Define `result_c: str` with text 'Rick Martinez' sliced from `DATA_C`
# 5. Define `result_d: str` with text 'Alex Vogel' sliced from `DATA_D`
# 6. Define `result_e: str` with text 'Beth Johanssen' sliced from `DATA_E`
# 7. Define `result_f: str` with text 'Chris Beck' sliced from `DATA_F`
# 8. Use `slice` to extract text
# 10. Run doctests - all must succeed
# %% Polish
# 1. Z każdej zmiennej `DATA_*` usuń tytuł naukowy i stopnia wojskowego
# 2. Define `result_a: str` z tekstem 'Mark Watney' wyciętym z `DATA_A`
# 3. Define `result_b: str` z tekstem 'Melissa Lewis' wyciętym z `DATA_B`
# 4. Define `result_c: str` z tekstem 'Rick Martinez' wyciętym z `DATA_C`
# 5. Define `result_d: str` z tekstem 'Alex Vogel' wyciętym z `DATA_D`
# 6. Define `result_e: str` z tekstem 'Beth Johanssen' wyciętym z `DATA_E`
# 7. Define `result_f: str` z tekstem 'Chris Beck' wyciętym z `DATA_F`
# 8. Użyj `slice` to wycięcia tekstu
# 10. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is str, \
'Variable `result_a` has invalid type, should be str'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is str, \
'Variable `result_b` has invalid type, should be str'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable `result_c`'
>>> assert type(result_c) is str, \
'Variable `result_c` has invalid type, should be str'
>>> assert result_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert type(result_d) is str, \
'Variable `result_d` has invalid type, should be str'
>>> assert result_e is not Ellipsis, \
'Assign your result to variable `result_e`'
>>> assert type(result_e) is str, \
'Variable `result_e` has invalid type, should be str'
>>> assert result_f is not Ellipsis, \
'Assign your result to variable `result_f`'
>>> assert type(result_f) is str, \
'Variable `result_f` has invalid type, should be str'
>>> result_a
'Mark Watney'
>>> result_b
'Melissa Lewis'
>>> result_c
'Rick Martinez'
>>> result_d
'Alex Vogel'
>>> result_e
'Beth Johanssen'
>>> result_f
'Chris Beck'
"""
EXAMPLE = 'lt. Mark Watney, PhD'
example = EXAMPLE[4:-5]
DATA_A = 'prof. Mark Watney'
DATA_B = 'Melissa Lewis, PhD'
DATA_C = 'lt. col. Rick Martinez'
DATA_D = 'dr Alex Vogel'
DATA_E = 'Beth Johanssen\t'
DATA_F = 'dr Chris Beck, MD-PhD'
# Define `result_a: str` with text 'Mark Watney' sliced from `DATA_A`
# Use `slice` to extract text
# type: str
result_a = ...
# Define `result_b: str` with text 'Melissa Lewis' sliced from `DATA_B`
# Use `slice` to extract text
# type: str
result_b = ...
# Define `result_c: str` with text 'Rick Martinez' sliced from `DATA_C`
# Use `slice` to extract text
# type: str
result_c = ...
# Define `result_d: str` with text 'Alex Vogel' sliced from `DATA_D`
# Use `slice` to extract text
# type: str
result_d = ...
# Define `result_e: str` with text 'Beth Johanssen' sliced from `DATA_E`
# Use `slice` to extract text
# type: str
result_e = ...
# Define `result_f: str` with text 'Chris Beck' sliced from `DATA_F`
# Use `slice` to extract text
# type: str
result_f = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Unpack Slice Substr
# - Difficulty: easy
# - Lines: 3
# - Minutes: 5
# %% English
# 1. Use `str.find()`, `len()` and slicing
# 2. Print `TEXT` without fragment from `REMOVE`
# 3. Output should be: 'We choose the Moon!'
# 4. Do not use `str.replace()`
# 5. Run doctests - all must succeed
# %% Polish
# 1. Użyj `str.find()`, `len()` oraz wycinania
# 2. Wypisz `TEXT` bez fragmentu znajdującego się w `REMOVE`
# 3. Wynik powinien być: 'We choose the Moon!'
# 4. Nie używaj `str.replace()`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> result
'We choose the Moon!'
"""
TEXT = 'We choose to go to the Moon!'
REMOVE = 'to go to '
# String with TEXT without REMOVE part
# type: str
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Unpack Slice Sequence
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% English
# 1. Create set `result` with every second element from `a` and `b`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Stwórz zbiór `result` z co drugim elementem `a` i `b`
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is set, \
'Variable `result` has invalid type, should be set'
>>> result
{0, 2, 4}
"""
a = (0, 1, 2, 3)
b = [2, 3, 4, 5]
# Set with every second element from `a` and `b`
# type: set[int]
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Unpack Slice Header/Rows
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% English
# 1. Separate header (first line) from rows
# 1. Define `header: tuple[str]` with a header (first line of `DATA`)
# 2. Define `rows: list[tuple]` with rows (all the other lines of `DATA`)
# 3. Use slice, i.e.: `data[1:5]`
# 4. Non-functional requirements:
# - Use only indexes (`getitem`)
# - Do not use `slice`, `str.split()`, `for`, `while` etc.
# - You can use copy and paste or vertically select `alt`
# 5. Run doctests - all must succeed
# %% Polish
# 1. Odseparuj nagłówek (pierwsza linia) od danych
# 1. Zdefiniuj `header: tuple[str]` z nagłówkiem (pierwsza linia `DATA`)
# 2. Zdefiniuj `rows: list[tuple]` z wierszami (wszystkie inne linie `DATA`)
# 3. Nie używaj slice, tj. `data[1:5]`
# 4. Wymagania niefunkcjonalne:
# - Korzystaj tylko z indeksów (`getitem`)
# - Nie używaj `str.split()`, `slice`, `for`, `while` itp.
# - Możesz użyć kopiowania i wklejania oraz zaznaczania pionowego `alt`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert header is not Ellipsis, \
'Assign your result to variable `header`'
>>> assert rows is not Ellipsis, \
'Assign your result to variable `rows`'
>>> assert type(header) is tuple, \
'Variable `header` has invalid type, should be tuple'
>>> assert all(type(x) is tuple for x in rows), \
'All elements in `rows` should be tuple'
>>> assert header not in rows, \
'Header should not be in `rows`'
>>> pprint(header)
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species')
>>> pprint(rows)
[(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')]
"""
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'),
]
# Define `header: tuple[str]` with a header (first line of `DATA`)
# type: tuple[str]
header = ...
# Define `rows: list[tuple]` with rows (all the other lines of `DATA`)
# Use slice, i.e.: `data[1:5]`
# type: list[tuple]
rows = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Unpack Slice Train/Test
# - Difficulty: easy
# - Lines: 4
# - Minutes: 8
# %% English
# 1. Divide `rows` into two lists:
# - `train`: 60% - training data
# - `test`: 40% - testing data
# 2. Calculate split point:
# - `rows` length multiplied by percent
# - From `rows` slice training data from start to split
# - From `rows` slice test data from split to end
# 3. Run doctests - all must succeed
# %% Polish
# 1. Podziel `rows` na dwie listy:
# - `train`: 60% - dane do uczenia
# - `test`: 40% - dane do testów
# 2. Aby to zrobić wylicz punkt podziału:
# - Długość `rows` razy procent
# - Z `rows` wytnij do uczenia rekordy od początku do punktu podziału
# - Z `rows` zapisz do testów rekordy od punktu podziału do końca
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert split is not Ellipsis, \
'Assign your result to variable `split`'
>>> assert train is not Ellipsis, \
'Assign your result to variable `train`'
>>> assert test is not Ellipsis, \
'Assign your result to variable `test`'
>>> assert type(split) is int, \
'Variable `split` has invalid type, should be int'
>>> assert type(train) is list, \
'Variable `train` has invalid type, should be list'
>>> assert type(train) is list, \
'Variable `train` has invalid type, should be list'
>>> assert type(test) is list, \
'Variable `test` has invalid type, should be list'
>>> assert all(type(x) is tuple for x in train), \
'All elements in `train` should be tuple'
>>> assert all(type(x) is tuple for x in test), \
'All elements in `test` should be tuple'
>>> pprint(split)
6
>>> pprint(train)
[(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')]
>>> pprint(test)
[(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')]
"""
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'),
]
header = DATA[0]
rows = DATA[1:]
# Result of `rows` length multiplied by percent
# type: int
split = ...
# List with first 60% from rows
# type: list[tuple]
train = ...
# List with last 40% from rows
# type: list[tuple]
test = ...