Add list to dictionary Python
Ngày đăng:
19/12/2021
Trả lời:
0
Lượt xem:
125
In this article we will discuss different ways to convert a single or multiple lists to dictionary in Python. Show
Following conversions from list to dictionary will be covered here,
Convert a List to Dictionary with same valuesSuppose we have a list of strings i.e. 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.Lets see how to do that i.e. Using dictionary comprehension Dictionary contents will be, hello :: 5 here :: 5 this :: 5 test :: 5 at :: 5 now :: 5 Using dict.fromKeys() ''' 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. Advertisements Convert List items as keys in dictionary with enumerated valueSuppose we have a list of strings i.e. Lets 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 dictionarySuppose we have two lists i.e. Lets 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,
Convert a list of tuples to dictionarySuppose we have a list of tuples with two columns in each entry i.e. 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:
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, 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 Advertisements
|