Alterna · учебный материал

Lists and processing of elements

38.1: Lists and element processing: Short theory, one example and Python step-by-step.

Что в этом материале

Содержание и основные понятия

The list is changed and the elements are stored in order. One passage is often sufficient for an examination without saving all the data, but the list is useful for sorting and re-analyzing.
substance and practice
Grade 9
EEG No. 17
EEG No. 26
01
How it works.
The list is a variable sequence. It fits when data need to be stored, changed, sorted or bypassed several times.
Creation.
Literal [1, 2] creates a list; list(...) builds a list from an sterilized object.
Index.
It's like a line, but the list can be replaced.
Change.
append, extend, sort and index assignment change the same list.
A copy.
a[:] and a.copy() create a new top list; b = a creates the middle name of the same object.
02
One example in execution
Start the short program and go over the lines. The visualiser only shows the code, current line, variables and output.
Pick a bypass if you don't need an index:
for value in values
If you need a position, use it.
enumerate
Do not remove elements from the list during direct bypassing: the indices are shifted and a part of the data may be omitted. Create a new filtered list or bypass a copy.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Create List
3
append adds one object
4
extraend adds elements
5
Check List
03
Remember
append([2,3]) adds one list, extend(,3] - two numbers.
Check the empty result before max, min and pop.
For an independent copy, don't use the middle name b = a.
Syntax of this page
list(iterable)
Creates a list from an iterated object.
append(x)
Adds one element x to the end of the list.
extend(iterable)
Adds all elements of iterable to the end, rather than iterabel itself as one element.
insert(i, x)
Inserts x before item i.
pop(i=-1)
Removes and returns the element; the empty list calls IndexError.
remove(x)
Removes the first equal x; the missing x causes ValueError.
index(x)
Returns the first x or ValueError position if x is missing.
count(x)
Considers equal x elements.
copy()
Returns a small copy of the list; the lists remain common objects.
enumerate(values)
Gives the index and the value.
[expr for x in values if cond]
Listing builds a new list.
len / sum
The length and sum of the numbers on the list.
map / split / int
Read the numbers from the same entry line.

Structural map of textbooks