List () yapıcısı Python'da bir liste döndürür.
Sözdizimi list()
şöyledir:
liste ((yinelenebilir))
list () Parametreler
list()
Yapıcı tek bir argüman alır:
- yinelenebilir (isteğe bağlı) - bir dizi (dize, tuples) veya koleksiyon (küme, sözlük) veya herhangi bir yineleyici nesne olabilen bir nesne
Listeden () değer döndür
list()
Yapıcı bir listesini döndürür.
- Hiçbir parametre geçilmezse, boş bir liste döndürür
- Yinelenebilir bir parametre olarak aktarılırsa, yinelenebilir öğelerden oluşan bir liste oluşturur.
Örnek 1: Dize, tuple ve listeden listeler oluşturun
# empty list print(list()) # vowel string vowel_string = 'aeiou' print(list(vowel_string)) # vowel tuple vowel_tuple = ('a', 'e', 'i', 'o', 'u') print(list(vowel_tuple)) # vowel list vowel_list = ('a', 'e', 'i', 'o', 'u') print(list(vowel_list))
Çıktı
() ('a', 'e', 'i', 'o', 'u') ('a', 'e', 'i', 'o', 'u') ('a', ' e ',' ben ',' o ',' u ')
Örnek 2: Küme ve sözlükten listeler oluşturun
# vowel set vowel_set = ('a', 'e', 'i', 'o', 'u') print(list(vowel_set)) # vowel dictionary vowel_dictionary = ('a': 1, 'e': 2, 'i': 3, 'o':4, 'u':5) print(list(vowel_dictionary))
Çıktı
('a', 'o', 'u', 'e', 'i') ('o', 'e', 'a', 'u', 'i')
Not: Sözlükler söz konusu olduğunda, sözlüğün anahtarları listenin öğeleri olacaktır. Ayrıca, elemanların sırası rastgele olacaktır.
Örnek 3: Bir yineleyici nesnesinden bir liste oluşturma
# objects of this class are iterators 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))
Çıktı
(1, 2, 4, 8, 16)
Önerilen Okuma: Python Listesi