Python Zip Dictionary: How to Convert List to Dictionary

Python Zip Dictionary: How to Convert List to Dictionary

Python Zip Dictionary: How to Convert List to Dictionary using zip()

The list and dictionary are among the robust data structures in Python. Converting from each other is the most common task you ever face in Python development. You often find that you have to create a dictionary from two different but closely related sequences. An easy way to approach this is to use the dict() and zip() methods together.

Python Zip Dictionary

To create a dictionary from two sequences, use the dict() and zip() method. In Python 3, the zip() method now returns a lazy iterator, which is now the most used approach.

The dict(zip(keys, values)) need the one-time global lookup each for dict and zip. Still, it doesn’t create any needless intermediary data-structures or have to deal with local lookups in function application.

## app.py

stocks = ['reliance', 'infosys', 'tcs']
prices = [2175, 1127, 2750]
dictionary = dict(zip(stocks, prices))
print(dictionary)
Output
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}

In this example, we have defined two lists that we need to convert into a dictionary. The first list of items will be keys for the dictionary, and the second list of items will be the dictionary.

The zip(fields, values) method returns an iterator that generates two-items tuples. If you call dict() on that iterator, then you can create the dictionary you need. The items of the first list become the dictionary’s keys, and the elements second list represent the values in the dictionary.

One important thing to note that dictionary = {zip(keys, values)} will not work. You have to declare as dict(zip()) explicitly. If you make it work, then you have to use proper dictionary comprehension.

python development programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?