Q:
Write a python program to map two lists into a dictionary
belongs to collection: Python Exercises, Practice Questions and Solutions
Python Exercises, Practice Questions and Solutions
- Write a program in Python to use the string join operation to create a string that contains a colon as a separator
- Write a program in Python to replace a, b with 1, 3 respectively in a string
- Write a program in Python to encode and decode the following string
- Write a program in Python to read content from one file and write in another file.
- Write a program in Python to create a dictionary from the given sequence of keys and value provided by the user
- Write a list comprehension that returns all the pairs in a dictionary whose associated values are greater than zero
- Write a program in Python to keep count of this given list starting from 100
- Write a program in Python to calculate the value of the following expression by using lambda function
- Write a program in Python to calculate the Fahrenheit of the following Celsius list by using Lambda function
- Write a program in Python to filter the given list if the given expression evaluates to true
- Write a Python program to count the occurrences of each word in a given sentence
- Write a program to calculate simple interest in Python
- Write a python program to calculate total marks percentage and grade of a student
- Write a python program to find the area of a rectangle in python
- Write a program to find the largest of three numbers in Python
- How do you transpose a matrix using list comprehension in Python?
- How do you make a simple calculator in Python?
- How do you find the prime factor of a number in Python?
- Write a program to read two numbers and print their quotient and remainder in Python
- Write a python program to sort words in alphabetical order
- Write a python program to sum all the numbers in a list
- How to apply filters to images using Python and OpenCV?
- Write a python program to input week number and print week day
- Write a python program to map two lists into a dictionary
- Python program to multiply two numbers
- Write a python program to remove last element from list
- How do you find the GCF in Python?
- How to multiply all elements in list Python?
- How do you count consonants in a string in python?
- How to read JSON from URL requests in python?
- Write a node.js program for making external http calls
Map two lists into a dictionary using zip() function
The zip() function of Python makes an iterator that aggregate elements from at least two lists. To map two lists, we can use the Python zip() function. This function allows us to combine two lists together. We can use one list as the keys for the dictionary and the other as the values.
In the given example, we have two lists, one containing a list of students and the other containing their marks. The requirement is to create a single dictionary that stores the name of a student along with their marks. We can use the given solution to accomplish this task:
Output of the above code:
{'Smith': 89, 'John': 53, 'Priska': 92, 'Abhi': 86}
If the lists vary in size, this method will truncate the longer list. If the keys are not unique, this method will pick the last value for mapping.
Map two lists into a dictionary using Dictionary Comprehension
Dictionary comprehension is one way to create a dictionary in Python. It creates a dictionary by combining two sets of data which are in the form of either lists or arrays. It is an elegant and concise approach to create dictionaries. Dictionary comprehension uses pointed brackets ({})
In the given code, we have used the dictionary comprehension to map two lists into a dictionary:
Output of the above code:
{'pen': 5, 'paper': 20, 'pencil': 15}
Map two lists into a dictionary using For loop
We can also use the for loop to map two lists into a single dictionary. The following example demonstrates this. First, we have initialized two lists fruits and price and then used for loop to map these two lists into a dictionary.
Output of the above code:
{'mango': 90, 'pear': 78, 'apple': 110, 'papaya': 60}
Map two lists into a dictionary using for and range
Here, we have used the for loop and range to map two lists into a single dictionary.
Output of the above code:
need an explanation for this answer? contact us directly to get an explanation for this answerList of Students : ['Smith', 'John', 'Priska', 'Abhi'] list of Marks : [93, 89, 91, 88] Dictionary from lists : {'Smith': 93, 'John': 89, 'Priska': 91, 'Abhi': 88}