Answers for "python make dictionary based on list"

1

python make dictionary based on list

lst = [('the', 7), ('car', 3), ('and', 3), ('tent', 2)]
newdct = {}
[newdct.update({k: v}) for k, v in lst]
Posted by: Guest on May-01-2022
1

create a dictionary from a list python

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'}
Posted by: Guest on February-25-2022
0

get values from list of dictionaries python

[d['value'] for d in l]
Posted by: Guest on February-17-2022

Code answers related to "python make dictionary based on list"

Python Answers by Framework

Browse Popular Code Answers by Language