Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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'}
 
PREVIOUS NEXT
Tagged: #create #dictionary #list #python
ADD COMMENT
Topic
Name
3+8 =