English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Le constructeur list() retourne une liste en Python.
La syntaxe de list() est :
list([iterable])
Le constructeur list() accepte un paramètre :
iterable(optionnel) -Un objet, qui peut être une séquence (chaîne,tuple)ou ensemble (ensemble,dictionnaire)ou tout objet itérable
Le constructeur list() retourne une liste.
Si aucun paramètre n'est passé, une liste vide est retournée
Si iterable est passé en paramètre, il crée une liste composée des éléments de iterable.
# Liste vide print(list()) # Chaîne de voyelles vowel_string = 'aeiou' print(list(vowel_string)) # Groupe de voyelles vowel_tuple = ('a', 'e', 'i', 'o', 'u') print(list(vowel_tuple)) # List of vowels vowel_list = ['a', 'e', 'i', 'o', 'u'] print(list(vowel_list))
Output result
[] ['a', 'e', 'i', 'o', 'u'] ['a', 'e', 'i', 'o', 'u'] ['a', 'e', 'i', 'o', 'u']
# Set of vowels vowel_set = {'a', 'e', 'i', 'o', 'u'} print(list(vowel_set)) # Dictionary of vowels vowel_dictionary = {'a': 1, 'e': 2, 'i': 3, 'o':4, 'u':5} print(list(vowel_dictionary))
Output result
['a', 'o', 'u', 'e', 'i'] ['o', 'e', 'a', 'u', 'i']
Note:For dictionaries, the keys of the dictionary will become the items of the list. Similarly, the order of the elements will be random.
# The object of this class is an iterator class PowTwo: def __init__(self, max): self.max = max def __iter__(self): self.num = 0 return self def __next__(self): if(self.num >= self.max): raise StopIteration result = 2 ** self.num self.num += 1 return result pow_two = PowTwo(5) pow_two_iter = iter(pow_two) print(list(pow_two_iter))
Output result
[1, 2, 4, 8, 16]
Recommended reading: Python lists Python built-in functions