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
Python program that renders a dictionary from two list with K values
A Dictionary is an unordered collection of data structure which stores the elements as key and value. It is mutable and in other programming languages Dictionaries are also known as associative arrays, hash maps, or hash tables. The key is unique and the values can be a single element or a list of elements with duplicates.
A Dictionary can be created using curly braces {} or by using the built-in function dict(). Let's see an example of creating a dictionary in Python ?
Example
student = {
'name': 'Tutorialspoint',
'age': 9,
'language': 'python'
}
print(student)
{'name': 'Tutorialspoint', 'age': 9, 'language': 'python'}
Using Python, we can create a dictionary from two lists where one list contains the keys and the other contains the values. There are several approaches to achieve this ?
Using for Loop
In this approach, we use a for loop to iterate through both lists simultaneously and create key-value pairs. We use the zip() function to combine corresponding elements from both lists.
The zip() function in Python is a built-in function that combines multiple iterables such as lists, tuples, or strings into an iterator of tuples. It pairs corresponding elements from each iterable together.
Example
We iterate over two lists simultaneously using the zip function. In each iteration, we assign elements from keys to the variable key and elements from values to the variable value, then create the key-value pair in the result dictionary ?
keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = {}
for key, value in zip(keys, values):
result[key] = value
print("The rendered dictionary:", result)
The rendered dictionary: {'a': 1, 'b': 2, 'c': 3}
Using Dictionary Comprehension
Dictionary comprehension is a concise and elegant way to create dictionaries in Python. It allows us to create dictionaries by specifying key-value pairs within a single line of code ?
The syntax of dictionary comprehension is ?
{key_expression: value_expression for item in iterable}
Example
We use dictionary comprehension to iterate over two lists using zip(). In each iteration, we create a key-value pair in the dictionary ?
keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = {key: value for key, value in zip(keys, values)}
print("The rendered dictionary:", result)
The rendered dictionary: {'a': 1, 'b': 2, 'c': 3}
Using the Built-in dict() Function
In this approach, we use the dict() function along with zip() to create a dictionary. The dict() function takes the zipped pairs as input and directly converts them into a dictionary ?
Example
keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = dict(zip(keys, values))
print("The rendered dictionary:", result)
The rendered dictionary: {'a': 1, 'b': 2, 'c': 3}
Handling Lists with Different Lengths
When lists have different lengths, zip() stops at the shortest list ?
Example
keys = ['a', 'b', 'c', 'd']
values = [1, 2, 3]
result = dict(zip(keys, values))
print("Dictionary with unequal lists:", result)
Dictionary with unequal lists: {'a': 1, 'b': 2, 'c': 3}
Comparison
| Method | Code Length | Readability | Best For |
|---|---|---|---|
| for Loop | Multiple lines | Very clear | Complex logic needed |
| Dictionary Comprehension | One line | Concise | Simple transformations |
| dict() + zip() | One line | Most readable | Direct conversion |
Conclusion
Use dict(zip()) for the most readable and efficient approach. Dictionary comprehension works well for simple transformations, while for loops provide flexibility for complex logic.
