Search
 
SCRIPT & CODE EXAMPLE
 

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'}
Comment

creating a dictionary from lists

a = [10,20,30]
b = [100,200,300]
my_dictionary = dict(a=a, b=b)
Comment

PREVIOUS NEXT
Code Example
Python :: create empty numpy array without shape 
Python :: How to join two dataframes by 2 columns so they have only the common rows? 
Python :: django queryset group by 
Python :: how to run python program in sublime text 3 windows 
Python :: run for loop inside pdb 
Python :: Converting Dataframe from the multi-dimensional list 
Python :: image crop in python 
Python :: discord py check if user has permission return message if not 
Python :: python pandas csv append 
Python :: remove new line character from string python 
Python :: calculator in python 
Python :: virtualenv python2 
Python :: create qr code in python 
Python :: roman to integer 
Python :: python sort the values in a dictionaryi 
Python :: d-tale colab 
Python :: string print in pattern in python 
Python :: deep copy a dataframe 
Python :: count most frequent words in list python 
Python :: pydrive upload file to folder 
Python :: numpy expand_dims 
Python :: Download video from a direct URL with Python 
Python :: random python between 0 and 1 
Python :: windows error message python 
Python :: add new row to numpy array 
Python :: convert all colnames of dataframe to upper 
Python :: plotting roc curve 
Python :: buttons on canvas tkinter 
Python :: python generate list 
Python :: variable string in string python 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =