Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a dictionary with list comprehension in Python?
Python provides several ways to create dictionaries using list comprehension. The dict() method combined with list comprehension offers an elegant approach to generate key-value pairs dynamically.
Syntax
The basic syntax for creating a dictionary with list comprehension ?
# Using dict() with list comprehension
dict([(key, value) for item in iterable])
# Direct dictionary comprehension (alternative)
{key: value for item in iterable}
Using Unicode Characters as Keys
Create a dictionary where keys are Unicode characters and values are their corresponding integers ?
dict_obj = dict([(chr(i), i) for i in range(100, 105)])
print('Output dictionary:', dict_obj)
print(type(dict_obj))
Output dictionary: {'d': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104}
<class 'dict'>
Using zip() with Two Lists
Combine two lists into a dictionary using zip() and list comprehension ?
keys = [1, 2, 3, 4, 5]
values = [10, 20, 30, 40, 50]
print('Keys list:', keys)
print('Values list:', values)
# Create dictionary using list comprehension
result_dict = dict([(key, value) for key, value in zip(keys, values)])
print('Output dictionary:', result_dict)
print(type(result_dict))
Keys list: [1, 2, 3, 4, 5]
Values list: [10, 20, 30, 40, 50]
Output dictionary: {1: 10, 2: 20, 3: 30, 4: 40, 5: 50}
<class 'dict'>
Creating Key-Value Pairs with Mathematical Operations
Generate a dictionary where values are calculated based on keys ?
tuple_list = [(i, i*2) for i in range(1, 10)]
print("List of tuples:", tuple_list)
result_dict = dict(tuple_list)
print('Output dictionary:', result_dict)
print(type(result_dict))
List of tuples: [(1, 2), (2, 4), (3, 6), (4, 8), (5, 10), (6, 12), (7, 14), (8, 16), (9, 18)]
Output dictionary: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
<class 'dict'>
Transforming List Values
Create a dictionary by transforming existing list values ?
numbers = [20, 21, 65, 29, 76, 98, 35]
print('Input list:', numbers)
# Create dictionary with transformed keys
result_dict = dict([(val/2, val) for val in numbers])
print('Output dictionary:', result_dict)
print(type(result_dict))
Input list: [20, 21, 65, 29, 76, 98, 35]
Output dictionary: {10.0: 20, 10.5: 21, 32.5: 65, 14.5: 29, 38.0: 76, 49.0: 98, 17.5: 35}
<class 'dict'>
Direct Dictionary Comprehension
Python also supports direct dictionary comprehension syntax without using dict() ?
# Direct dictionary comprehension
squares = {x: x**2 for x in range(1, 6)}
print('Squares dictionary:', squares)
# Filtering with condition
even_squares = {x: x**2 for x in range(1, 11) if x % 2 == 0}
print('Even squares:', even_squares)
Squares dictionary: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Even squares: {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
Conclusion
Dictionary creation with list comprehension provides a concise way to generate key-value pairs dynamically. Use dict([(key, value) for ...]) with tuples or the direct {key: value for ...} syntax for cleaner code.
