Python | Convert two lists into a dictionary - GeeksforGeeks

28 Nov, 2018
Interconversion between data types are usually necessary in real time applications as certain systems have certain modules which require the input in a particular data-type. Let’s discuss a simple yet useful utility of conversion of two lists into a key:value pair dictionary.
Method #1 : Naive MethodThe basic method that can be applied to perform this task is the brute force method to achieve this. For this, simply declare a dictionary, and then run nested loop for both the lists and assign key and value pairs to from list values to dictionary.
# Python3 code to demonstrate # conversion of lists to dictionary# using naive method  # initializing liststest_keys = ["Rash", "Kil", "Varsha"]test_values = [1, 4, 5]  # Printing original keys-value listsprint ("Original key list is : " + str(test_keys))print ("Original value list is : " + str(test_values))  # using naive method# to convert lists to dictionaryres = {}for key in test_keys:    for value in test_values:        res[key] = value        test_values.remove(value)        break    # Printing resultant dictionary print ("Resultant dictionary is : " +  str(res))
Original key list is : ['Rash', 'Kil', 'Varsha']
Original value list is : [1, 4, 5]
Resultant dictionary is : {'Varsha': 5, 'Rash': 1, 'Kil': 4}

 Method #2 : Using dictionary comprehensionThe more concise way to achieve the above method, dictionary comprehension method offers the faster and time-saving approach by reducing the lines to type.
# Python3 code to demonstrate # conversion of lists to dictionary# using dictionary comprehension  # initializing liststest_keys = ["Rash", "Kil", "Varsha"]test_values = [1, 4, 5]  # Printing original keys-value listsprint ("Original key list is : " + str(test_keys))print ("Original value list is : " + str(test_values))  # using dictionary comprehension# to convert lists to dictionaryres = {test_keys[i]: test_values[i] for i in range(len(test_keys))}  # Printing resultant dictionary print ("Resultant dictionary is : " +  str(res))
Original key list is : ['Rash', 'Kil', 'Varsha']
Original value list is : [1, 4, 5]
Resultant dictionary is : {'Varsha': 5, 'Kil': 4, 'Rash': 1}

 Method #3 : Using zip()Most pythonic and generic method to perform this very task is by using zip(). This function pairs the list element with other list element at corresponding index in form of key-value pairs.
# Python3 code to demonstrate # conversion of lists to dictionary# using zip()  # initializing liststest_keys = ["Rash", "Kil", "Varsha"]test_values = [1, 4, 5]  # Printing original keys-value listsprint ("Original key list is : " + str(test_keys))print ("Original value list is : " + str(test_values))  # using zip()# to convert lists to dictionaryres = dict(zip(test_keys, test_values))  # Printing resultant dictionary print ("Resultant dictionary is : " +  str(res))
Original key list is : ['Rash', 'Kil', 'Varsha']
Original value list is : [1, 4, 5]
Resultant dictionary is : {'Kil': 4, 'Rash': 1, 'Varsha': 5}

Python dictionary-programs
python-dict
python-list
Python
python-dict
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Convert integer to string in Python