Two of the most common data structures in Python are lists and dictionaries. If you take a look back at your code, you may notice that you have a lot of lists and dictionaries used all over the place.
They may look quite similar, but they have a key difference. You can think of a list as a collection of items, and a dictionary as a collection of key-value pairs.
There can be cases when you may need to do a conversion of 2 lists into a single dictionary. This is something that you can easily do in Python with the help of zip() function.
First, let’s take a look at 2 lists:
first = [“name”, “surname”, “city”]
second = [“Durim”, “Gashi”, “New York”]
Now, we can simply call the zip() function:
Yes, it’s that easy.
Note that you can use this even if you have duplicate elements or one of the lists has more elements than the other one, for example: