6.2. Iterable List
Mutable - can add, remove, and modify items
Stores elements of any type
CPython's lists are really variable-length arrays, not Lisp-style linked lists. The implementation uses a contiguous array of references to other objects, and keeps a pointer to this array and the array's length in a list head structure.
This makes indexing a list data[i]
an operation whose cost is independent
of the size of the list or the value of the index.
When items are appended or inserted, the array of references is resized. Some cleverness is applied to improve the performance of appending items repeatedly; when the array must be grown, some extra space is allocated so the next few times don't require an actual resize.
6.2.1. Define Empty
data = []
- emptylist
data = list()
- emptylist
data = []
is faster thandata = list()
Empty list
:
>>> data = []
>>> data = list()
Performance:
>>> %%timeit -r 1000 -n 1000 # doctest: +SKIP
... data = []
...
8.13 ns ± 3.26 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
8.27 ns ± 2.51 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
8.19 ns ± 2.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
7.74 ns ± 3.48 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
7.97 ns ± 1.86 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> %%timeit -r 1000 -n 1000 # doctest: +SKIP
... data = list()
...
23.3 ns ± 6.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
22.3 ns ± 4.43 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
22.4 ns ± 3.97 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
22.6 ns ± 4.47 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
22.8 ns ± 4.08 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
Date: 2025-01-09
Python: 3.13.1
IPython: 8.31.0
System: macOS 15.2
Computer: MacBook M3 Max
CPU: 16 cores (12 performance and 4 efficiency) / 3nm
RAM: 128 GB RAM LPDDR5
6.2.2. Define With Elements
Can store elements of any types
Single element
list
does not require comma at the enddata = [1,]
- list
Can store elements of any types:
>>> 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]
Brackets are required
>>> data = [1, 2, 3]
Comma after last element is optional:
>>> data = [1]
>>> data = [1,]
6.2.3. Conversion
list()
converts argument tolist
Takes one iterable as an argument
Multiple arguments are not allowed
Builtin function list()
converts argument to list
>>> text = 'hello'
>>> list(text)
['h', 'e', 'l', 'l', 'o']
>>> colors = ['red', 'green', 'blue']
>>> list(colors)
['red', 'green', 'blue']
>>> colors = ('red', 'green', 'blue')
>>> list(colors)
['red', 'green', 'blue']
>>> list('red', 'green', 'blue')
Traceback (most recent call last):
TypeError: list expected at most 1 argument, got 3
6.2.4. Count
list.count()
- number of occurrences of value
>>> colors = ('red', 'green', 'blue', 'red')
>>>
>>> colors.count('red')
2
6.2.5. Index
list.index()
- position at which something is in the listNote, that Python start counting at zero (zero based indexing)
Raises
ValueError
if the value is not present
>>> colors = ('red', 'green', 'blue')
>>>
>>>
>>> colors.index('red')
0
>>>
>>> colors.index('green')
1
>>>
>>> colors.index('blue')
2
6.2.6. Pop
list.pop()
Can take position
If position is not given, pops last element
Returns element
>>> colors = ['red', 'green', 'blue']
>>> result = colors.pop(1)
>>>
>>>
>>> print(colors)
['red', 'blue']
>>>
>>> print(result)
green
>>> colors = ['red', 'green', 'blue']
>>> result = colors.pop()
>>>
>>>
>>> print(colors)
['red', 'green']
>>>
>>> print(result)
blue
6.2.7. Remove
list.remove(obj)
returns
None
>>> colors = ['red', 'green', 'blue']
>>> result = colors.remove('red')
>>>
>>>
>>> print(colors)
['green', 'blue']
>>>
>>> print(result)
None
6.2.8. Clear
list.clear()
>>> colors = ['red', 'green', 'blue']
>>> result = colors.clear()
>>>
>>>
>>> print(colors)
[]
>>>
>>> print(result)
None
6.2.9. Append
list.append()
- adds one element at the end of alist
O(1)
complexity
>>> colors = ['red', 'green', 'blue']
>>> colors.append('black')
>>>
>>> print(colors)
['red', 'green', 'blue', 'black']
6.2.10. Extend
list.extend()
- adds many elements at the end of alist
O(1)
complexity
>>> colors = ['red', 'green', 'blue']
>>> colors.extend(['black', 'white'])
>>>
>>> print(colors)
['red', 'green', 'blue', 'black', 'white']
6.2.11. Add and IAdd
list + list
- addlist += list
- increment add
Add:
>>> colors = ['red', 'green', 'blue']
>>> result = colors + ['black']
>>>
>>> print(colors)
['red', 'green', 'blue']
>>>
>>> print(result)
['red', 'green', 'blue', 'black']
Increment Add:
>>> colors = ['red', 'green', 'blue']
>>> colors += ['black']
>>>
>>> print(colors)
['red', 'green', 'blue', 'black']
Errors:
>>> colors + 'black'
Traceback (most recent call last):
TypeError: can only concatenate list (not "str") to list
>>> colors = ['red', 'green', 'blue']
>>> colors += 'black'
>>>
>>> print(colors)
['red', 'green', 'blue', 'b', 'l', 'a', 'c', 'k']
6.2.12. Insert
list.insert(idx, object)
- insert object at specific positionShift other elements by one
O(n)
complexity
>>> colors = ['red', 'green', 'blue']
>>> colors.insert(0, 'black')
>>>
>>> print(colors)
['black', 'red', 'green', 'blue']
>>> colors = ['red', 'green', 'blue']
>>> colors.insert(1, 'black')
>>>
>>> print(colors)
['red', 'black', 'green', 'blue']
6.2.13. Reverse
list.reverse()
>>> colors = ['red', 'green', 'blue']
>>> colors.reverse()
>>> colors
['blue', 'green', 'red']
6.2.14. Sort
sorted()
- returns new sorted list, but does not modify the originallist.sort()
- sorts list and returnsNone
Why doesn't list.sort() return the sorted list? [3]
In situations where performance matters, making a copy of the list just to sort it would be wasteful. Therefore, list.sort() sorts the list in place. In order to remind you of that fact, it does not return the sorted list. This way, you won't be fooled into accidentally overwriting a list when you need a sorted copy but also need to keep the unsorted version around.
If you want to return a new list, use the built-in sorted() function instead. This function creates a new list from a provided iterable, sorts it and returns it. For example, here's how to iterate over the keys of a dictionary in sorted order
Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was implemented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsequences of the data that are already ordered (runs) and uses them to sort the remainder more efficiently. This is done by merging runs until certain criteria are fulfilled. Timsort has been Python's standard sorting algorithm since version 2.3. It is also used to sort arrays of non-primitive type in Java SE 7, on the Android platform, in GNU Octave, on V8, Swift, and Rust. [1]
Worst-case performance: \(O(n\log{n})\)
Best-case performance: \(O(n)\)
Average performance: \(O(n\log{n})\)
Worst-case space complexity: \(O(n)\)
sorted()
- Returns sorted list, do not modify the originallist.sort()
- Changes object permanently, returnsNone
Return sorted values without modifying a list:
>>> values = [3, 1, 2]
>>>
>>> sorted(values)
[1, 2, 3]
>>>
>>> sorted(values, reverse=True)
[3, 2, 1]
Permanent sorting with list modification (note that list.sort()
modifies
values, and returns None
, not values):
>>> values = [3, 1, 2]
>>>
>>> values.sort()
>>> values
[1, 2, 3]
>>>
>>> values.sort(reverse=True)
>>> values
[3, 2, 1]
You can also use list.sort()
and/or sorted()
with str
. It will
sort strings according to Unicode (UTF-8) value, that is ASCII table for
latin alphabet and Unicode for extended encoding. This kind of sorting is
called lexicographic order.
>>> colors = ['red', 'green', 'blue']
>>>
>>> sorted(colors)
['blue', 'green', 'red']
6.2.15. Built-in Functions
min()
- Minimal valuemax()
- Maximal valuesum()
- Sum of elementslen()
- Length of a list
List with numeric values:
>>> data = [3, 1, 2]
>>>
>>> len(data)
3
>>> min(data)
1
>>> max(data)
3
>>> sum(data)
6
List with string values:
>>> data = ['a', 'c', 'b']
>>>
>>> len(data)
3
>>> min(data)
'a'
>>> max(data)
'c'
>>> sum(data)
Traceback (most recent call last):
TypeError: unsupported operand type(s) for +: 'int' and 'str'
6.2.16. Shallow Copy vs. Deep Copy
Shallow Copy (by reference) - identifiers are pointing to the same object in memory
Deep Copy - identifiers are pointing to distinct objects
Shallow Copy is faster and requires less memory (no duplicated objects)
Deep Copy is slower and requires twice sa much memory, but is safe for modification
Shallow Copy:
>>> a = ['red', 'green', 'blue']
>>> b = a
>>>
>>> a.append('black')
>>>
>>> a
['red', 'green', 'blue', 'black']
>>> b
['red', 'green', 'blue', 'black']
>>>
>>> id(a)
4417433984
>>> id(b)
4417433984
Deep Copy:
>>> a = ['red', 'green', 'blue']
>>> b = a.copy()
>>>
>>> a.append('black')
>>>
>>> a
['red', 'green', 'blue', 'black']
>>> b
['red', 'green', 'blue']
>>>
>>> id(first)
4391796976
>>> id(second)
4391797008
6.2.17. Recap
Mutable - can add, remove, and modify items
Stores elements of any type
Extensible and flexible
6.2.18. References
6.2.19. 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: Iterable List Definition
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% English
# 1. Define `result_a` as a `list` without elements
# 2. Define `result_b` as a `list` with elements: 1, 2, 3
# 3. Define `result_c` as a `list` with elements: 1.1, 2.2, 3.3
# 4. Define `result_d` as a `list` with elements: 'a', 'b', 'c'
# 5. Define `result_e` as a `list` with elements: True, False, None
# 6. Define `result_f` as a `list` with elements: 1, 2.2, True, 'a'
# 7. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result_a` jako `list` bez elementów
# 2. Zdefiniuj `result_b` jako `list` z elementami: 1, 2, 3
# 3. Zdefiniuj `result_c` jako `list` z elementami: 1.1, 2.2, 3.3
# 4. Zdefiniuj `result_d` jako `list` z elementami: 'a', 'b', 'c'
# 5. Zdefiniuj `result_e` jako `list` z elementami: True, False, None
# 6. Zdefiniuj `result_f` jako `list` z elementami: 1, 2.2, True, 'a'
# 7. 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 result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable `result_c`'
>>> assert result_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert result_e is not Ellipsis, \
'Assign your result to variable `result_e`'
>>> assert result_f is not Ellipsis, \
'Assign your result to variable `result_f`'
>>> assert type(result_a) is list, \
'Variable `result_a` has invalid type, should be list'
>>> assert type(result_b) is list, \
'Variable `result_b` has invalid type, should be list'
>>> assert type(result_c) is list, \
'Variable `result_c` has invalid type, should be list'
>>> assert type(result_d) is list, \
'Variable `result_d` has invalid type, should be list'
>>> assert type(result_e) is list, \
'Variable `result_e` has invalid type, should be list'
>>> assert type(result_f) is list, \
'Variable `result_f` has invalid type, should be list'
>>> assert result_a == [], \
'Variable `result_a` has invalid value, should be []'
>>> assert result_b == [1, 2, 3], \
'Variable `result_b` has invalid value, should be [1, 2, 3]'
>>> assert result_c == [1.1, 2.2, 3.3], \
'Variable `result_c` has invalid value, should be [1.1, 2.2, 3.3]'
>>> assert result_d == ['a', 'b', 'c'], \
'Variable `result_d` has invalid value, should be ["a", "b", "c"]'
>>> assert result_e == [True, False, None], \
'Variable `result_e` has invalid value, should be [True, False, None]'
>>> assert result_f == [1, 2.2, True, 'a'], \
'Variable `result_f` has invalid value, should be [1, 2.2, True, "a"]'
"""
# Define `result_a` as a `list` without elements
# type: list
result_a = ...
# Define `result_b` as a `list` with elements: 1, 2, 3
# type: list[int]
result_b = ...
# Define `result_c` as a `list` with elements: 1.1, 2.2, 3.3
# type: list[float]
result_c = ...
# Define `result_d` as a `list` with elements: 'a', 'b', 'c'
# type: list[str]
result_d = ...
# Define `result_e` as a `list` with elements: True, False, None
# type: list[bool|None]
result_e = ...
# Define `result_f` as a `list` with elements: 1, 2.2, True, 'a'
# type: list[int|float|bool|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: Iterable List Insert
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Insert character 'x' at the beginning of `result`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Wstaw znak 'x' na początku `result`
# 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 list, \
'Variable `result` has invalid type, should be list'
>>> result
['x', 'a', 'b', 'c']
"""
result = ['a', 'b', 'c']
# Insert string 'x' at the beginning of `result`
# type: list
...
# %% 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: Iterable List Append
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Insert character 'x' at the end of `result`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Wstaw znak 'x' na końcu `result`
# 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 list, \
'Variable `result` has invalid type, should be list'
>>> result
['a', 'b', 'c', 'x']
"""
result = ['a', 'b', 'c']
# Insert string 'x' at the end of `result`
# type: list
...
# %% 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: Iterable List Extend
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Insert all characters from `data` at the end of `result`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Wstaw wszystkie znaki z `data` na końcu `result`
# 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 list, \
'Variable `result` has invalid type, should be list'
>>> result
['a', 'b', 'c', 'x', 'y']
"""
result = ['a', 'b', 'c']
data = ['x', 'y']
# Insert all characters from `data` at the end of `result`
# type: list
...
# %% 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: Iterable List Sort
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Sort `result`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Posortuj `result`
# 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 list, \
'Variable `result` has invalid type, should be list'
>>> result
['a', 'b', 'c']
"""
result = ['c', 'a', 'b']
# Sort `result`
# type: list
...
# %% 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: Iterable List Reverse
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Reverse order of `result` (do not sort)
# 2. Run doctests - all must succeed
# %% Polish
# 1. Odwróć kolejność `result` (nie sortuj)
# 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 list, \
'Variable `result` has invalid type, should be list'
>>> result
['b', 'a', 'c']
"""
result = ['c', 'a', 'b']
# Reverse order of `result` (do not sort)
# type: list
...
# %% 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: Iterable List Pop
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Delete last element from `result`
# 2. Do not use `getitem` or `slice`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Usuń ostatni element z `result`
# 2. Nie używaj `getitem` lub `slice`
# 3. 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 list, \
'Variable `result` has invalid type, should be list'
>>> assert 'x' not in result, \
'Variable `result` should not contain `x`'
>>> result
['a', 'b']
"""
result = ['a', 'b', 'c']
# Delete last element from `result`
# Do not use `getitem` or `slice`
# type: list
...
# %% 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: Iterable List Remove
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Delete element 'x' from `result`
# 2. Do not hardcode the position of `x`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Usuń element 'x' z `result`
# 2. Nie wpisuj na sztywno pozycji `x`
# 3. 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 list, \
'Variable `result` has invalid type, should be list'
>>> assert 'x' not in result, \
'Variable `result` should not contain `x`'
>>> result
['a', 'b', 'c']
"""
result = ['a', 'x', 'b', 'c']
# Delete element 'x' from `result`
# Do not hardcode the position of `x`
# type: list
...
# %% 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: Iterable List Clear
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Delete all elements from `result`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Usuń wszystkie elementy `result`
# 3. 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 list, \
'Variable `result` has invalid type, should be list'
>>> assert 'x' not in result, \
'Variable `result` should not contain `x`'
>>> result
[]
"""
result = ['a', 'b', 'c']
# Delete all elements from `result`
# type: list
...
# %% 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: Iterable List Copy
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define variables:
# - `result_a` with shallow copy of `data` (by reference)
# - `result_b` with deep copy of `data` (by value)
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienne:
# - `result_a` z płytką kopią `data` (przez referencję)
# - `result_b` z głęboką kopią `data` (przez wartość)
# 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_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_a) is list, \
'Variable `result_a` has invalid type, should be list'
>>> assert type(result_b) is list, \
'Variable `result_b` has invalid type, should be list'
>>> assert 'x' not in result_a, \
'Variable `result_a` should not contain `x`'
>>> assert 'x' not in result_b, \
'Variable `result_b` should not contain `x`'
>>> assert result_a == data, \
'Variable `result_a` should be identical to `data`'
>>> assert result_b == data, \
'Variable `result_b` should be identical to `data`'
>>> assert result_a is data, \
'Variable `result_a` should be shallow copy of `data`'
>>> assert result_b is not data, \
'Variable `result_b` shouldn be deep copy of `data`'
>>> result_a
['a', 'b', 'c']
>>> result_b
['a', 'b', 'c']
"""
data = ['a', 'b', 'c']
# Shallow copy of `data` (by reference)
# type: list
result_a = ...
# Deep copy of `data` (by value)
# type: list
result_b = ...