[] or list() |
empty list |
[a, b, c,] |
square brackets, separating items with commas |
[x for x in iterable] |
list comprehension |
list(iterable) |
type constructor |
list() |
[] |
|
list('abc') |
['a', 'b', 'c'] |
|
list( (1, 2, 3) ) |
[1, 2, 3] |
passare da tupla a lista |
list(range(4)) |
[0, 1, 2, 3] |
Many other operations also produce lists, es sorted() built-in.
list({'c':5,'a':2,'b':4}) -> ['c', 'a', 'b']
la lista di un dizionario e' la lista delle chiavi, i valori non sono considerati.
The constructor list() builds a list whose items are the same and in the same order as iterable’s items.
iterable may be
1: a sequence; 2: a container that supports iteration, es zip; 3: an iterator object.
If iterable is already a list, a copy is made and returned, similar to iterable[:].
Lists implement all of the common and mutable sequence operations.
sorted() Built-in Function.
Lists also provide the following additional method:
sort(*, key=None, reverse=False)
this method sorts the list in place.
{'c':5,'a':2,'b':4}.values() dict_values([5, 2, 4])
list(zip(['one', 'two', 'three'], [1, 2, 3]))
[('one', 1), ('two', 2), ('three', 3)]
[c for c in range(3)] [0, 1, 2]
[str(c) for c in range(3)] ['0', '1', '2']
[x for x in range(3) if x%2==0] [0, 2]
['pari' if x%2==0 else 'dispari' for x in range(3)] ['pari', 'dispari', 'pari']
[(x, y) for x in range(1, 4) for y in range(7, 9)] [(1, 7), (1, 8), (2, 7), (2, 8), (3, 7), (3, 8)]
[(x, y) for x in range(1, 4) for y in range(7, 9) if y%2==0]
[(1, 8), (2, 8), (3, 8)]
[(x, y) for x in range(1, 4) if x%2==0 for y in range(7, 9)]
[(2, 7), (2, 8)]
[(x, y) for x in range(1, 4) if x%2==0 for y in range(7, 9) if y%2==0]
[(2, 8)]
[range(i, i + 3) for i in [2, 4, 6]]
[range(2, 5), range(4, 7), range(6, 9)]
[list(range(i, i + 3)) for i in [2, 4, 6]]
[[2, 3, 4], [4, 5, 6], [6, 7, 8]]
LT lista di 2-tuple, cioe' coppie
LT =[('cespuglio', 1), ('sdrucciolarono', 1), ('rintronare', 1), ('potevano', 3), ('monticello', 1), ('nello', 6), ('strizzoni', 1), ('udirono', 2), ('compagne', 1), ('passandoci', 1), ('appena', 42), ('assistono', 2), ('fieramente', 1), ('scioccherie', 2), ('bella', 40), ('palla', 4), ('diventano', 3), ('fiammiferi', 2), ('pugni', 1), ('piace', 4)]
[e[0] for e in LT if e[1] == 1]
syntax
[f(x) for x in X if g(x)]
ref: library/random.html#functions-for-sequences
L e' una lista
equivale a
L[random.randrange(len(L))