TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python Program to Convert Lists into a Dictionary
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python ExamplesPython Tutorials

Python Program to Convert Lists into a Dictionary

Last updated: Nov 05, 2023 12:09 am
By Meenakshi Agarwal
Share
3 Min Read
Python Program To Convert Lists Into A Dictionary
Python Program To Convert Lists Into A Dictionary
SHARE

In this sample program, you will learn how to convert lists to a Python dictionary and show it using the print() function. To understand this demo program, you should have basic Python programming knowledge.

However, here we’ll use the following steps to convert lists into a dictionary.

  1. Define a pair of lists: one for the keys and one for the values.
  2. Set the lists as empty which means they should not have any elements.
  3. Then the program asks for the following inputs from the user.
    • First, it gets the number of elements that you want in the list.
    • After that, it uses a for loop which asks the user to supply inputs and get them added to the list of keys.
    • Next is another for loop which receives values from the end-user and keeps adding them to the list of values in each iteration.
  4. Finally, we call the Python zip method to convert lists into a dictionary.
  5. In the end, print the dictionary object.

Below is the sample code of the Python Program to convert lists into a dictionary using the Zip() method.

Python Program – Convert Lists into the Dictionary

You can use IDLE or any other Python IDE to create and execute the below programs.

Using the for loop to convert lists into a dictionary

# Program to Convert Lists into a Dictionary

# Pair of lists for storing the keys and values
listOfkeys = []
listOfvalues = []

count = int(input("Input total no. of elements in the lists:"))
print("Capture input for the keys:")

for item in range(0, count):
    elt = int(input("Input item" + str(item + 1) + ":"))
    listOfkeys.append(elt)

print("Capture input for the values:")

for item in range(0, count):
    elt = int(input("Input item" + str(item + 1) + ":"))
    listOfvalues.append(elt)

di = dict(zip(listOfkeys, listOfvalues))
print("The dictionary after the merge:")
print(di)

The output of the above code is as follows.

Input total no. of elements in the lists:5
Capture input for the keys:
Input item1:1
Input item2:2
Input item3:3
Input item4:4
Input item5:5
Capture input for the values:
Input item1:11
Input item2:22
Input item3:33
Input item4:44
Input item5:55
The dictionary after the merge:
{1: 11, 2: 22, 3: 33, 4: 44, 5: 55}

Using list comprehension

Create a dictionary from two separate lists, one containing keys and the other containing corresponding values. This example demonstrates how to use a list comprehension and the zip() function to pair the elements.

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

# {'a': 1, 'b': 2, 'c': 3}

Using enumerate with list comprehension

Convert a list into a dictionary where the list elements become keys, and their corresponding indices serve as values. This is achieved using enumerate() and a list comprehension.

my_list = ['apple', 'banana', 'cherry']
dictionary = {value: index for index, value in enumerate(my_list)}
print(dictionary)

# {'apple': 0, 'banana': 1, 'cherry': 2}

By the way, if you are seeking to learn more or planning for a job change, go through these 100+ Python interview questions. It will certainly rejuvenate your Python programming skills.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python program to Generate Fibonacci Sequence using Recursion Generate Fibonacci Sequence using Recursion
Next Article Python Format How To Format Data Types In Python Python String Format

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x