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 the unique one and the values are of a single element or a list of elements with duplicates.

A Dictionary can be created by using the curly braces {} or by using the built in function dict(). Let’s see an example of creating the dictionary in python.

Example

student = {
   'name': 'Tutorialspoint',
   'age': 9,
   'language': 'python'
}
print(student)

Output

{'name': 'Tutorialspoint', 'age': 9, 'language': 'python'}

By using python, we can render a dictionary from the elements of the two lists, in which one list contains the keys and the other list contains the values. There are several approaches to render the dictionary from the two lists, let’s see each one in detail.

Using for loops

In this approach, we are using for loop to render the lists into a dictionary by iterating both the list elements. We are also using the zip() function along with for loop to combine the key and value.

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 takes the corresponding elements from each iterable and pairs them together.

Example

In this example, we are iterating over the two lists simultaneously using the zip function. In every iteration we are assigning the elements from keys to the variable key and the elements from values to the variable value. Then, we are creating or updating the key-value pair in the result dictionary using result[key] = value.

keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = {}
for key, value in zip(keys, values):
   result[key] = value
print("The rendered dictionary:",result)

Output

The rendered dictionary: {'a': 1, 'b': 2, 'c': 3}

Using dictionary comprehension

A Dictionary comprehension is a concise and elegant way to create dictionaries in Python. It allows us to create dictionaries by specifying the key-value pairs within a single line of code. The syntax of a dictionary comprehension is as follows:

{key_expression: value_expression for item in iterable}

Example

In this example, we are using a dictionary comprehension and trying to iterate over the two lists using zip(). Then in each iteration, we are creating a key-value pair in the dictionary. The resulting dictionary is assigned to the variable result.

keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = {key: value for key, value in zip(keys, values)}
print("The rendered dictionary:",result)

Output

The rendered dictionary: {'a': 1, 'b': 2, 'c': 3}

Using the built-in dict() function

In this approach, we are using the dict() function along with the zip() to render the lists into a dictionary. The dict() function takes the zip() function as the input and the zip() function takes the two lists as the input parameters. Then finally returns the dictionary.

Example

keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = dict(zip(keys, values))
print("The rendered dictionary:",result)

Output

The rendered dictionary: {'a': 1, 'b': 2, 'c': 3}

Updated on: 02-Aug-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements