Python List is a linear data structure that is used to store data. In this topic, we will create a dictionary by using the list. Dictionary also a data structure that is used to store data in key-value pair.
Here we have some examples to explain conversion between list and dictionary data structure.
Lets start with a simple example in which we have a list of numeric items and converting it to the dictionary. We are checking type of data after converting as well. Look at the following example.
# Take a list
listItems = [2,3,6,8,11,20,3,40]
# Print the list
print("List items:\n",listItems, type(listItems))
# converting list to dictionary
dic = dict.fromkeys(listItems)
print("Dictionary Elements:")
for element in dic :
print(element, end = ' ')
print( type(dic))
Output:
List items:
[2, 3, 6, 8, 11, 20, 40] <class 'list'>
Dictionary Elements:
2 3 6 8 11 20 40 <class 'dict'>
Notice: The newly created dictionary contains only unique items from the list. It is because dictionary contains unique keys only but allows duplicate values.
If we iterate the dictionary in key-value pair then it will show all the value by default are set to none.
# Take a list
listItems = [2,3,6,8,11,20,3,40]
# Print the list
print("List items:\n",listItems, type(listItems))
# converting list to dictionary
dic = dict.fromkeys(listItems)
print("Dictionary Elements:")
print(dic, type(dic))
Output:
List items:
[2, 3, 6, 8, 11, 20, 3, 40] <class 'list'>
Dictionary Elements:
{2: None, 3: None, 6: None, 8: None, 11: None, 20: None, 40: None} <class 'dict'>
In a scenario, where we have two lists and want to convert them into a dictionary then zip() function is suitable. Python zip() is a function that is used to combine two iterable objects and returns a list of tuples.
Here, we have passed two lists in zip() function to get a list of tuples, where each tuple contains an entry from both the lists. After that we created a dictionary object from this list of tuples.
# Take a list
list1 = ["One","Two","Three","Four"]
# Print the list
print("List1 items:\n",list1, type(list1))
# Take another list
list2 = [1,2,3,4]
print("List2 items:\n",list2, type(list2))
# converting list to dictionary
ziplists = zip(list1,list2)
dic = dict(ziplists)
print("Dictionary Elements:")
print(dic, type(dic))
Output:
List1 items:
['One', 'Two', 'Three', 'Four'] <class 'list'>
List2 items:
[1, 2, 3, 4] <class 'list'>
Dictionary Elements:
{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4} <class 'dict'>
If we already have a list of tuples then we don't need to use zip() function that we did in above example. We can directly pass this list into the dict() function and get the dictionary. See the below example.
# Take a list
listOfTuples = [(1,"One"),(2,"Two"),(3,"Three"),(4,"Four")]
# Print the list
print("List items:\n",listOfTuples, type(listOfTuples))
# converting list to dictionary
dic = dict(listOfTuples)
print("Dictionary Elements:")
print(dic, type(dic))
Output:
List items:
[(1, 'One'), (2, 'Two'), (3, 'Three'), (4, 'Four')] <class 'list'>
Dictionary Elements:
{1: 'One', 2: 'Two', 3: 'Three', 4: 'Four'} <class 'dict'>
Well, in this topic, we learnt to convert python list to dictionary. We used single list and multiple lists to convert into dictionary. In an example, we used zip() function to combine two lists and then convert to dictionary. We have used several example to explain the conversion process.