Python membuat kamus berdasarkan daftar
lst = [('the', 7), ('car', 3), ('and', 3), ('tent', 2)]
newdct = {}
[newdct.update({k: v}) for k, v in lst]
Edi Sugiarto
lst = [('the', 7), ('car', 3), ('and', 3), ('tent', 2)]
newdct = {}
[newdct.update({k: v}) for k, v in lst]
fruits = ["Apple", "Pear"]
# Create a dictionary using as keys the values in fruits list
fruit_dictionary = dict.fromkeys(fruits, "In Stock")
print(fruit_dictionary) # {'Apple': 'In Stock', 'Pear': 'In Stock'}
# Alternatively, dictionary comprehension can be used for same purpose
fruit_dictionary = { fruit : "In stock" for fruit in fruits }
print(fruit_dictionary) # {'Apple': 'In Stock', 'Pear': 'In Stock'}
[d['value'] for d in l]