- Index starts with
0
.
- Slice:
x[1:4]
gives elements fromx[1]
tox[3]
inclusive (takes1
, not4
).
x[:3] + x[3:]
gives exactlyx
.
Ordered (different order, different list):
Mutable (we can change elements in list),
Directly,
From other types,
With
for
(List comprehensions),Create a list from range,
A list of random int numbers,
Don't use
y = x
directly!Normal list (1 dimensional),
Nested list,
Add to desired positions,
With slices (it likes the intersection between indexes of the current list with indexes indicated in the slice):
Using the keyword
del
:Using
.remove()
to remove a value in list (it removes the first found value):If you wanna remove all specific value from a list:
Using
.pop()
to remove and return the deleted element.Using
.clear()
to empty a list:Special case, using a empty list:
Using
+
and *
(repeat),Return a sorted list but not change the list:
Sort and change a list:
Sorted with a key function:
If you wanna apply a function to each element in an iterable:
Get an element from a list of dictionaries wich condition:
.count(<e>)
: Returns the number of item<e>
in list.