Kamus Python fromkeys() Metode
Contoh
Buat kamus dengan 3 kunci, semua dengan nilai 0:
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)
Definisi dan Penggunaan
Metode fromkeys()
mengembalikan kamus dengan kunci yang ditentukan dan nilai yang ditentukan.
Sintaksis
dict.fromkeys(keys, value)
Nilai Parameter
Parameter | Description |
---|---|
keys | Required. An iterable specifying the keys of the new dictionary |
value | Optional. The value for all keys. Default value is None |
Lebih Banyak Contoh
Contoh
Contoh yang sama seperti di atas, tetapi tanpa menentukan nilainya:
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)