Convert List of Lists to Dictionary - Python
Last Updated :
12 Jul, 2025
We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}.
Using Dictionary Comprehension
Using dictionary comprehension, we iterate over each sublist in the list of lists, unpacking the first element as the key and the second as the value. This creates a dictionary efficiently in a single line of code.
Python
# List of lists where each sublist contains a key-value pair
a = [["a", 1], ["b", 2], ["c", 3]]
# Use dictionary comprehension to create a dictionary
# Unpack each sublist into key and value, and map them to the dictionary
res = {key: value for key, value in a}
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation:
- Dictionary comprehension iterates through each sublist in
a, unpacking the first element as the key and the second as the value. - It constructs a dictionary by mapping each key to its corresponding value, resulting in
{'a': 1, 'b': 2, 'c': 3}.
Using dict()
To convert a list of lists into a dictionary using dict(), we can pass the list of lists to the dict() constructor.
Python
# List of lists where each sublist contains a key-value pair
a = [["a", 1], ["b", 2], ["c", 3]]
# Convert the list of lists into a dictionary using the dict() function
# Each sublist is unpacked into key and value pairs
res = dict(a)
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation:
- Code converts a list of lists into a dictionary using the
dict() function, where each sublist represents a key-value pair. dict() function takes the list of lists as an argument and returns a dictionary with keys "a", "b", and "c", and their respective values 1, 2, and 3.
Using zip()
We can use the zip() function to pair corresponding elements from two lists, creating an iterable of tuples. By passing this result to the dict() function, we can easily convert it into a dictionary, where the first list becomes the keys and the second list becomes the values.
Python
keys = ["a", "b", "c"]
values = [1, 2, 3]
# Use the zip() function to pair keys with values, then convert the result into a dictionary using dict()
res = dict(zip(keys, values))
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation:
zip() function combines the keys and values lists by pairing corresponding elements together, creating tuples like ("a", 1), ("b", 2), and ("c", 3).dict() function then converts these pairs into a dictionary, resulting in {'a': 1, 'b': 2, 'c': 3}.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice