
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Convert two lists into a dictionary in Python
While a Python list contains a series of values a dictionary on the other hand contains a pair of values which are called key-value pairs. In this article we will take two lists and mark them together to create a Python dictionary.
With for and remove
We create two nested for loops. In the inner loop will assign one of the list as key for the dictionary while keep removing the values from the list which is outer for loop.
Example
listK = ["Mon", "Tue", "Wed"] listV = [3, 6, 5] # Given lists print("List of K : ", listK) print("list of V : ", listV) # Empty dictionary res = {} # COnvert to dictionary for key in listK: for value in listV: res[key] = value listV.remove(value) break print("Dictionary from lists :\n ",res)
Output
Running the above code gives us the following result −
('List of K : ', ['Mon', 'Tue', 'Wed']) ('list of V : ', [3, 6, 5]) ('Dictionary from lists :\n ', {'Wed': 5, 'Mon': 3, 'Tue': 6})
With for and range
The two lists are combined to create a pair of values by putting them into a for loop. The range and len functions are used to keep track of the number of elements till all the key value pairs are created.
Example
listK = ["Mon", "Tue", "Wed"] listV = [3, 6, 5] # Given lists print("List of K : ", listK) print("list of V : ", listV) # COnvert to dictionary res = {listK[i]: listV[i] for i in range(len(listK))} print("Dictionary from lists :\n ",res)
Output
Running the above code gives us the following result −
('List of K : ', ['Mon', 'Tue', 'Wed']) ('list of V : ', [3, 6, 5]) ('Dictionary from lists :\n ', {'Wed': 5, 'Mon': 3, 'Tue': 6})
With zip
The zip function does something similar to above approach. It also combines the elements from the two lists, creating key and value pairs.
Example
listK = ["Mon", "Tue", "Wed"] listV = [3, 6, 5] # Given lists print("List of K : ", listK) print("list of V : ", listV) # COnvert to dictionary res = dict(zip(listK, listV)) print("Dictionary from lists :\n ",res)
Output
Running the above code gives us the following result −
('List of K : ', ['Mon', 'Tue', 'Wed']) ('list of V : ', [3, 6, 5]) ('Dictionary from lists :\n ', {'Wed': 5, 'Mon': 3, 'Tue': 6})
- Related Articles
- How to map two lists into a dictionary in Python?
- Python - Convert a set into dictionary
- Python Convert nested dictionary into flattened dictionary?
- Python - Convert flattened dictionary into nested dictionary
- Convert a list into tuple of lists in Python
- Convert dictionary object into string in Python
- Convert list into list of lists in Python
- In Python how to create dictionary from two lists?
- Python – Cross mapping of Two dictionary value lists
- How I can convert a Python Tuple into Dictionary?
- Python - Convert a list of lists into tree-like dict
- Python program to convert a list of tuples into Dictionary
- Python - Convert list of nested dictionary into Pandas Dataframe
- Ways to create a dictionary of lists in python
- Python - Ways to create a dictionary of Lists
