In this article we will discuss different ways to convert a single or multiple lists to dictionary in Python.
Following conversions from list to dictionary will be covered here,
- Convert a List to Dictionary with same values
- Convert List items as keys in dictionary with enumerated value
- Convert two lists to dictionary
- Convert a list of tuples to dictionary
Convert a List to Dictionary with same values
Suppose we have a list of strings i.e.
# List of strings listOfStr = ["hello", "at" , "test" , "this" , "here" , "now" ]
Now we want to create a dictionary with all elements in this list as keys. For each key value will be same i.e. 5. Let’s see how to do that i.e.
Using dictionary comprehension
''' Converting a list to dictionary with list elements as keys in dictionary All keys will have same value ''' dictOfWords = { i : 5 for i in listOfStr }
Dictionary contents will be,
hello :: 5 here :: 5 this :: 5 test :: 5 at :: 5 now :: 5
Using dict.fromKeys()
Frequently Asked:
''' Converting a list to dictionary with list elements as keys in dictionary using dict.fromkeys() ''' dictOfWords = dict.fromkeys(listOfStr , 1)
dict.fromKeys() accepts a list and default value. It returns a dictionary with items in list as keys. All dictionary items will have same value, that was passed in fromkeys().
If no default value was passed in fromKeys() then default value for keys in dictionary will be None.
Convert List items as keys in dictionary with enumerated value
Suppose we have a list of strings i.e.
# List of strings listOfStr = ["hello", "at" , "test" , "this" , "here" , "now" ]
Let’s create a dictionary from this list with list elements as keys and values as integers from 0 to n-1 (n is size of list) i.e.
''' Converting a list to dictionary with list elements as values in dictionary and keys are enumerated index starting from 0 i.e. index position of element in list ''' dictOfWords = { i : listOfStr[i] for i in range(0, len(listOfStr) ) }
Dictionary contents will be,
0 :: hello 1 :: at 2 :: test 3 :: this 4 :: here 5 :: now
Convert two lists to a dictionary
Suppose we have two lists i.e.
# List of strings listOfStr = ["hello", "at" , "test" , "this" , "here" , "now" ] # List of ints listOfInt = [56, 23, 43, 97, 43, 102]
Let’s create a dictionary with elements of listOfStr as keys and elements of listOfInt as values using zip() i.e
# Create a zip object from two lists zipbObj = zip(listOfStr, listOfInt) # Create a dictionary from zip object dictOfWords = dict(zipbObj)
Zip() accepts a number of iterable objects and returns a list of tuples. Each entry in tuple contains an element from each iterable object.
We have passed two lists objects in zip() , so it will return a list of tuples, where each tuple contains an entry from both the lists. Then we created a dictionary object from this list of tuples.
Dictionary contents are,
Dictionary from two Lists hello :: 56 here :: 43 this :: 97 test :: 43 at :: 23 now :: 102
- If length of keys list is less than list of values then remaining elements in value list will be skipped.
Convert a list of tuples to dictionary
Suppose we have a list of tuples with two columns in each entry i.e.
# List of tuples listofTuples = [("Riti" , 11), ("Aadi" , 12), ("Sam" , 13),("John" , 22),("Lucy" , 90)]
We can directly pass this list of tuples to dictionary constructor i.e
# Convert a list of tuple to dictionary studentsDict = dict(listofTuples)
Entries in first column will become the key and entries in second column will the values in the new dictionary. Contents of dictionary will be,
Dictionary from List of Tuples John :: 22 Lucy :: 90 Riti :: 11 Aadi :: 12 Sam :: 13
Python Dictionary Tutorial - Series:
- What is a Dictionary in Python & why do we need it?
- Creating Dictionaries in Python
- Iterating over dictionaries
- Check if a key exists in dictionary
- Check if a value exists in dictionary
- Get all the keys in Dictionary
- Get all the Values in a Dictionary
- Remove a key from Dictionary
- Add key/value pairs in Dictionary
- Find keys by value in Dictionary
- Filter a dictionary by conditions
- Print dictionary line by line
- Convert a list to dictionary
- Sort a Dictionary by key
- Sort a dictionary by value in descending or ascending order
- Dictionary: Shallow vs Deep Copy
- Remove keys while Iterating
- Get all keys with maximum value
- Merge two or more dictionaries in python
Subscribe with us to join a list of 2000+ programmers and get latest tips & tutorials at your inbox through our weekly newsletter.
Complete example is as follows,
''' Display contents of dictionary with each key/value pair in seperate line ''' def displatDict(text, dictOfElements) : print("*************") print(text) for key , value in dictOfElements.items(): print(key, " :: ", value) def main(): # List of strings listOfStr = ["hello", "at" , "test" , "this" , "here" , "now" ] ''' Converting a list to dictionary with list elements as keys in dictionary All keys will have same value ''' dictOfWords = { i : 5 for i in listOfStr } displatDict("Dictionary with same value " , dictOfWords) ''' Converting a list to dictionary with list elements as keys in dictionary using dict.fromkeys() ''' dictOfWords = dict.fromkeys(listOfStr , 1) displatDict("Dictionary with given default value " , dictOfWords) dictOfWords = dict.fromkeys(listOfStr) displatDict("Dictionary with same default value None " , dictOfWords) ''' Converting a list to dictionary with list elements as values in dictionary and keys are enumerated index starting from 0 i.e. index position of element in list ''' dictOfWords = { i : listOfStr[i] for i in range(0, len(listOfStr) ) } displatDict("Dictionary with enumerated keys" , dictOfWords) ''' Converting multiple lists to dictionary using zip list1 will be used as keys and list2 as values ''' # List of strings listOfStr = ["hello", "at" , "test" , "this" , "here" , "now" ] # List of ints listOfInt = [56, 23, 43, 97, 43, 102] # Create a zip object from two lists zipbObj = zip(listOfStr, listOfInt) # Create a dictionary from zip object dictOfWords = dict(zipbObj) displatDict("Dictionary from two Lists " , dictOfWords) ''' If list of keys is greater than list of values then extra keys will be skipped ''' # List of strings listOfStr = ["hello", "at" , "test" , "this" , "here" , "now" ] # List of ints listOfInt = [56, 23, 43, 97, 43] zipbObj = zip(listOfStr, listOfInt) dictOfWords = dict(zipbObj) displatDict("Dictionary from two Lists " , dictOfWords) ''' Convert a list of tuples to Dictionary ''' # List of tuples listofTuples = [("Riti" , 11), ("Aadi" , 12), ("Sam" , 13),("John" , 22),("Lucy" , 90)] # Convert a list of tuple to dictionary studentsDict = dict(listofTuples) displatDict("Dictionary from List of Tuples" , studentsDict) if __name__ == '__main__': main()
Output
************* Dictionary with same value now :: 5 here :: 5 test :: 5 at :: 5 this :: 5 hello :: 5 ************* Dictionary with given default value now :: 1 here :: 1 test :: 1 at :: 1 this :: 1 hello :: 1 ************* Dictionary with same default value None now :: None here :: None test :: None at :: None this :: None hello :: None ************* Dictionary with enumerated keys 0 :: hello 1 :: at 2 :: test 3 :: this 4 :: here 5 :: now ************* Dictionary from two Lists now :: 102 here :: 43 test :: 43 at :: 23 this :: 97 hello :: 56 ************* Dictionary from two Lists test :: 43 at :: 23 this :: 97 hello :: 56 here :: 43 ************* Dictionary from List of Tuples John :: 22 Lucy :: 90 Riti :: 11 Aadi :: 12 Sam :: 13
THANK YOU, IVE BENE TRYING TO SOLVE SOMETHING IN MY CLASS FOR 5 HOURS YOU HAVE NO IDEA WHAT YOU MEAN TO ME I LOVE YOU THIS FIXED EVERYTHING