Movatterモバイル変換


[0]ホーム

URL:


— FREE Email Series —

🐍 Python Tricks 💌

Python Tricks Dictionary Merge

🔒 No spam. Unsubscribe any time.

Browse TopicsGuided Learning Paths
Basics Intermediate Advanced
aialgorithmsapibest-practicescareercommunitydatabasesdata-sciencedata-structuresdata-vizdevopsdjangodockereditorsflaskfront-endgamedevguimachine-learningnewsnumpyprojectspythonstdlibtestingtoolsweb-devweb-scraping

Table of Contents

Recommended Course

Using the Python zip() Function for Parallel Iteration

Parallel Iteration With Python's zip() Function

35m · 6 lessons

Using the Python zip() Function for Parallel Iteration

Using the Python zip() Function for Parallel Iteration

byLeodanis Pozo RamosReading time estimate 18mbasicspython

Table of Contents

Remove ads

Recommended Course

Parallel Iteration With Python's zip() Function(35m)

Python’szip() function combines elements from multiple iterables. Callingzip() generates an iterator that yields tuples, each containing elements from the input iterables. This function is essential for tasks like parallel iteration and dictionary creation, offering an efficient way to handle multiple sequences in Python programming.

By the end of this tutorial, you’ll understand that:

  • zip() in Python aggregates elements from multiple iterables into tuples, facilitatingparallel iteration.
  • dict(zip())creates dictionaries by pairing keys and values from two sequences.
  • zip() islazy in Python, meaning it returns an iterator instead of a list.
  • There’s nounzip() function in Python, but the samezip() function canreverse the process using the unpacking operator*.
  • Alternatives tozip() includeitertools.zip_longest() for handling iterables ofunequal lengths.

In this tutorial, you’ll explore how to usezip() for parallel iteration. You’ll also learn how to handle iterables of unequal lengths and discover the convenience of usingzip() with dictionaries. Whether you’re working with lists, tuples, or other data structures, understandingzip() will enhance your coding skills and streamline your Python projects.

Free Bonus:5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Understanding the Pythonzip() Function

zip() is available in thebuilt-in namespace. If you usedir() to inspect__builtins__, then you’ll seezip() at the end of the list:

Python
>>>dir(__builtins__)['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']

You can see that'zip' is the last entry in the list of available objects.

According to theofficial documentation, Python’szip() function behaves as follows:

Returns an iterator of tuples, where thei-th tuple contains thei-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. (Source)

You’ll unpack this definition throughout the rest of the tutorial. As you work through the code examples, you’ll see that Python zip operations work just like the physical zipper on a bag or pair of jeans. Interlocking pairs of teeth on both sides of the zipper are pulled together to close an opening. In fact, this visual analogy is perfect for understandingzip(), since the function was named after physical zippers!

Usingzip() in Python

The signature of Python’szip() function iszip(*iterables, strict=False). You’ll learn more aboutstrict later. The function takes initerables as arguments and returns aniterator. This iterator generates a series of tuples containing elements from each iterable.zip() can accept any type of iterable, such asfiles,lists, tuples,dictionaries,sets, and so on.

Passingn Arguments

If you usezip() withn arguments, then the function will return an iterator that generates tuples of lengthn. To see this in action, take a look at the following code block:

Python
>>>numbers=[1,2,3]>>>letters=["a","b","c"]>>>zipped=zip(numbers,letters)>>>zipped# Holds an iterator object<zip object at 0x7fa4831153c8>>>>type(zipped)<class 'zip'>>>>list(zipped)[(1, 'a'), (2, 'b'), (3, 'c')]

Here, you usezip(numbers, letters) to create an iterator that produces tuples of the form(x, y). In this case, thex values are taken fromnumbers and they values are taken fromletters. Notice how the Pythonzip() function returns an iterator. To retrieve the final list object, you need to uselist() to consume the iterator.

If you’re working with sequences like lists, tuples, orstrings, then your iterables are guaranteed to be evaluated from left to right. This means that the resulting list of tuples will take the form[(numbers[0], letters[0]), (numbers[1], letters[1]),..., (numbers[n], letters[n])]. However, for other types of iterables (likesets), you might see some weird results:

Python
>>>s1={2,3,1}>>>s2={"b","a","c"}>>>list(zip(s1,s2))[(1, 'a'), (2, 'c'), (3, 'b')]

In this example,s1 ands2 areset objects, which don’t keep their elements in any particular order. This means that the tuples returned byzip() will have elements that are paired up randomly. If you’re going to use the Pythonzip() function with unordered iterables like sets, then this is something to keep in mind.

Passing No Arguments

You can callzip() with no arguments as well. In this case, you’ll simply get an empty iterator:

Python
>>>zipped=zip()>>>zipped<zip object at 0x7f196294a488>>>>list(zipped)[]

Here, you callzip() with no arguments, so yourzippedvariable holds an empty iterator. If you consume the iterator withlist(), then you’ll see an empty list as well.

You could also try to force the empty iterator to yield an element directly. In this case, you’ll get aStopIterationexception:

Python
>>>zipped=zip()>>>next(zipped)Traceback (most recent call last):  File"<stdin>", line1, in<module>StopIteration

When you callnext() onzipped, Python tries to retrieve the next item. However, sincezipped holds an empty iterator, there’s nothing to pull out, so Python raises aStopIteration exception.

Passing One Argument

Python’szip() function can take just one argument as well. The result will be an iterator that yields a series of 1-item tuples:

Python
>>>a=[1,2,3]>>>zipped=zip(a)>>>list(zipped)[(1,), (2,), (3,)]

This may not be that useful, but it still works. Perhaps you can find some use cases for this behavior ofzip()!

As you can see, you can call the Pythonzip() function with as many input iterables as you need. The length of the resulting tuples will always equal the number of iterables you pass as arguments. Here’s an example with three iterables:

Python
>>>integers=[1,2,3]>>>letters=["a","b","c"]>>>floats=[4.0,5.0,6.0]>>>zipped=zip(integers,letters,floats)# Three input iterables>>>list(zipped)[(1, 'a', 4.0), (2, 'b', 5.0), (3, 'c', 6.0)]

Here, you call the Pythonzip() function with three iterables, so the resulting tuples have three elements each.

Passing Arguments of Unequal Length

When you’re working with the Pythonzip() function, it’s important to pay attention to the length of your iterables. It’s possible that the iterables you pass in as arguments aren’t the same length.

In these cases, the number of elements thatzip() puts out will be equal to the length of theshortest iterable. The remaining elements in any longer iterables will be totally ignored byzip(), as you can see here:

Python
>>>list(zip(range(5),range(100)))[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

Since5 is the length of the first (and shortest)range() object,zip() outputs a list of five tuples. There are still 95 unmatched elements from the secondrange() object. These are all ignored byzip() since there are no more elements from the firstrange() object to complete the pairs.

If trailing or unmatched values are important to you, then you can useitertools.zip_longest() instead ofzip(). With this function, the missing values will be replaced with whatever you pass to thefillvalue argument (defaults toNone). The iteration will continue until the longest iterable is exhausted:

Python
>>>fromitertoolsimportzip_longest>>>numbers=[1,2,3]>>>letters=["a","b","c"]>>>longest=range(5)>>>zipped=zip_longest(numbers,letters,longest,fillvalue="?")>>>list(zipped)[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]

Here, you useitertools.zip_longest() to yield five tuples with elements fromletters,numbers, andlongest. The iteration only stops whenlongest is exhausted. The missing elements fromnumbers andletters are filled with a question mark?, which is what you specified withfillvalue.

SincePython 3.10,zip() has a new optional keyword argument calledstrict, which was introduced throughPEP 618—Add Optional Length-Checking To zip. This argument’s main goal is to provide asafe way to handleiterables of unequal length.

The default value ofstrict isFalse, which ensures thatzip() remains backward compatible and has a default behavior that matches its behavior in older Python 3 versions:

Python
>>>list(zip(range(5),range(100)))[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

In Python 3.10 and later, callingzip() without altering the default value tostrict still gives you a list of five tuples, with the unmatched elements from the secondrange() object ignored.

Alternatively, if you setstrict toTrue, thenzip() checks if the input iterables you provided as arguments have the same length, raising aValueError if they don’t:

Python
>>>list(zip(range(5),range(100),strict=True))Traceback (most recent call last):  File"<stdin>", line1, in<module>ValueError:zip() argument 2 is longer than argument 1

This new feature ofzip() is useful when you need to make sure that the function only accepts iterables of equal length. Settingstrict toTrue makes code that expects equal-length iterables safer, ensuring that faulty changes to the caller code don’t result in silently losing data.

Looping Over Multiple Iterables

Looping over multiple iterables is one of the most common use cases for Python’szip() function. If you need to iterate through multiple lists, tuples, or any other sequence, then it’s likely that you’ll fall back onzip(). This section will show you how to usezip() to iterate through multiple iterables at the same time.

Traversing Lists in Parallel

Python’szip() function allows you to iterate in parallel over two or more iterables. Sincezip() generates tuples, you can unpack these in the header of afor loop:

Python
>>>letters=["a","b","c"]>>>numbers=[0,1,2]>>>forletter,numberinzip(letters,numbers):...print(f"Letter:{letter}")...print(f"Number:{number}")...Letter: aNumber: 0Letter: bNumber: 1Letter: cNumber: 2

Here, you iterate through the series of tuples returned byzip() and unpack the elements intoletter andnumber. When you combinezip(),for loops, andtuple unpacking, you can get a useful andPythonic idiom for traversing two or more iterables at once.

You can also iterate through more than two iterables in a singlefor loop. Consider the following example, which has three input iterables:

Python
>>>letters=["a","b","c"]>>>numbers=[0,1,2]>>>operators=["*","/","+"]>>>forlet,num,opinzip(letters,numbers,operators):...print(f"Letter:{let}")...print(f"Number:{num}")...print(f"Operator:{op}")...Letter: aNumber: 0Operator: *Letter: bNumber: 1Operator: /Letter: cNumber: 2Operator: +

In this example, you usezip() with three iterables to create and return an iterator that generates 3-item tuples. This lets you iterate through all three iterables in one go. There’s no restriction on the number of iterables you can use with Python’szip() function.

Note: If you want to dive deeper into Pythonfor loops, check outPython “for” Loops (Definite Iteration).

Traversing Dictionaries in Parallel

In Python 3.6 and beyond, dictionaries areordered collections, meaning they keep their elements in the same order in which they were introduced. If you take advantage of this feature, then you can use the Pythonzip() function to iterate through multiple dictionaries in a safe and coherent way:

Python
>>>dict_one={"name":"John","last_name":"Doe","job":"Python Consultant"}>>>dict_two={"name":"Jane","last_name":"Doe","job":"Community Manager"}>>>for(k1,v1),(k2,v2)inzip(dict_one.items(),dict_two.items()):...print(k1,"->",v1)...print(k2,"->",v2)...name -> Johnname -> Janelast_name -> Doelast_name -> Doejob -> Python Consultantjob -> Community Manager

Here, you iterate throughdict_one anddict_two in parallel. In this case,zip() generates tuples with the items from both dictionaries. Then, you can unpack each tuple and gain access to the items of both dictionaries at the same time.

Note: If you want to dive deeper into dictionary iteration, check outHow to Iterate Through a Dictionary in Python.

Notice that, in the above example, the left-to-right evaluation order is guaranteed. You can also use Python’szip() function to iterate through sets in parallel. However, you’ll need to consider that, unlike dictionaries in Python 3.6, setsdon’t keep their elements in order. If you forget this detail, the final result of your program may not be quite what you want or expect.

Unzipping a Sequence

There’s a question that comes up frequently in forums for new Pythonistas: “If there’s azip() function, then why is there nounzip() function that does the opposite?”

The reason why there’s nounzip() function in Python is because the opposite ofzip() is… well,zip(). Do you recall that the Pythonzip() function works just like a real zipper? The examples so far have shown you how Python zips things closed. So, how do you unzip Python objects?

Say you have a list of tuples and want to separate the elements of each tuple into independent sequences. To do this, you can usezip() along with theunpacking operator*, like so:

Python
>>>pairs=[(1,"a"),(2,"b"),(3,"c"),(4,"d")]>>>numbers,letters=zip(*pairs)>>>numbers(1, 2, 3, 4)>>>letters('a', 'b', 'c', 'd')

Here, you have alist of tuples containing some kind of mixed data. Then, you use the unpacking operator* to unzip the data, creating two different lists (numbers andletters).

Sorting in Parallel

Sorting is a common operation in programming. Suppose you want to combine two lists and sort them at the same time. To do this, you can usezip() along with.sort() as follows:

Python
>>>letters=["b","a","d","c"]>>>numbers=[2,4,3,1]>>>data1=list(zip(letters,numbers))>>>data1[('b', 2), ('a', 4), ('d', 3), ('c', 1)]>>>data1.sort()# Sort by letters>>>data1[('a', 4), ('b', 2), ('c', 1), ('d', 3)]>>>data2=list(zip(numbers,letters))>>>data2[(2, 'b'), (4, 'a'), (3, 'd'), (1, 'c')]>>>data2.sort()# Sort by numbers>>>data2[(1, 'c'), (2, 'b'), (3, 'd'), (4, 'a')]

In this example, you first combine two lists withzip() and sort them. Notice howdata1 is sorted byletters anddata2 is sorted bynumbers.

You can also usesorted() andzip() together to achieve a similar result:

Python
>>>letters=["b","a","d","c"]>>>numbers=[2,4,3,1]>>>sorted(zip(letters,numbers))# Sort by letters[('a', 4), ('b', 2), ('c', 1), ('d', 3)]

In this case,sorted() runs through the iterator generated byzip() and sorts the items byletters, all in one go. This approach can be a little bit faster since you’ll need only two function calls:zip() andsorted().

Withsorted(), you’re also writing a more general piece of code. This will allow you to sort any kind of sequence, not just lists.

Calculating in Pairs

You can use the Pythonzip() function to make some quick calculations. Suppose you have the following data in a spreadsheet:

Element/MonthJanuaryFebruaryMarch
Total Sales52,000.0051,000.0048,000.00
Production Cost46,800.0045,900.0043,200.00

You’re going to use this data to calculate your monthly profit.zip() can provide you with a fast way to make the calculations:

Python
>>>total_sales=[52000.00,51000.00,48000.00]>>>prod_cost=[46800.00,45900.00,43200.00]>>>forsales,costsinzip(total_sales,prod_cost):...profit=sales-costs...print(f"Total profit:{profit}")...Total profit: 5200.0Total profit: 5100.0Total profit: 4800.0

Here, you calculate the profit for each month by subtractingcosts fromsales. Python’szip() function combines the right pairs of data to make the calculations. You can generalize this logic to make any kind of complex calculation with the pairs returned byzip().

Building Dictionaries

Python’sdictionaries are a very useful data structure. Sometimes, you might need to build a dictionary from two different but closely related sequences. A convenient way to achieve this is to usedict() andzip() together. For example, suppose you retrieved a person’s data from a form or a database. Now you have the following lists of data:

Python
>>>fields=["name","last_name","age","job"]>>>values=["John","Doe","45","Python Developer"]

With this data, you need to create a dictionary for further processing. In this case, you can usedict() along withzip() as follows:

Python
>>>person=dict(zip(fields,values))>>>person{'name': 'John', 'last_name': 'Doe', 'age': '45', 'job': 'Python Developer'}

Here, you create a dictionary that combines the two lists.zip(fields, values) returns an iterator that generates 2-items tuples. If you calldict() on that iterator, then you’ll be building the dictionary you need. The elements offields become the dictionary’s keys, and the elements ofvalues represent the values in the dictionary.

You can also update an existing dictionary by combiningzip() withdict.update(). Suppose that John changes his job and you need to update the dictionary. You can do something like the following:

Python
>>>new_job=["Python Consultant"]>>>field=["job"]>>>person.update(zip(field,new_job))>>>person{'name': 'John', 'last_name': 'Doe', 'age': '45', 'job': 'Python Consultant'}

Here,dict.update() updates the dictionary with the key-value tuple you created using Python’szip() function. With this technique, you can easily overwrite the value ofjob.

Conclusion

In this tutorial, you’ve learned how to use Python’szip() function.zip() can receive multiple iterables as input. It returns an iterator that can generate tuples with paired elements from each argument. The resulting iterator can be quite useful when you need to process multiple iterables in a single loop and perform some actions on their items at the same time.

Now you can:

  • Use thezip() function in Python effectively
  • Loop over multiple iterables and perform different actions on their items in parallel
  • Create and update dictionaries on the fly by zipping two input iterables together

You’ve also coded a few examples that you can use as a starting point for implementing your own solutions using Python’szip() function. Feel free to modify these examples as you explorezip() in depth!

Frequently Asked Questions

Now that you have some experience with thezip() function in Python, you can use the questions and answers below to check your understanding and recap what you’ve learned. These frequently asked questions sum up the most important concepts you’ve covered in this tutorial. Click theShow/Hide toggle beside each question to reveal the answer.

Thezip() function takes multiple iterables as arguments and returns an iterator of tuples, where each tuple contains elements from the input iterables at the same index. The iteration stops when the shortest input iterable is exhausted. If called with a single iterable, it returns an iterator of 1-tuples, and with no arguments, it returns an empty iterator.

Whenzip() is used with iterables of different lengths, it stops creating tuples when the shortest iterable is exhausted. Any remaining elements in the longer iterables are ignored. However, you can useitertools.zip_longest() to handle this situation, which will fill missing values with a specifiedfillvalue.

dict(zip()) is a common pattern used to create dictionaries on the fly by zipping two iterables together. The first iterable provides the keys, and the second iterable provides the values. For example,dict(zip(["name", "age"], ("Alice", 30))) creates the dictionary{"name": "Alice", "age": 30}.

Yes,zip() is lazy in Python. It returns an iterator that generates tuples only as needed, rather than creating the entire list of tuples at once. This behavior is more memory efficient, especially when dealing with large datasets.

No, there isn’t a directunzip() function in Python, but you can achieve the same effect by using the unpacking operator* withzip(). For example,zip(*zipped) can be used to separate a list of tuples into individual sequences.

If you need to handle iterables of unequal length and want to ensure that all elements are included, you can useitertools.zip_longest(). This function continues until the longest iterable is exhausted, filling missing values with a specifiedfillvalue.

Recommended Course

Parallel Iteration With Python's zip() Function(35m)

🐍 Python Tricks 💌

Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Python Tricks Dictionary Merge

AboutLeodanis Pozo Ramos

Leodanis is a self-taught Python developer, educator, and technical writer with over 10 years of experience.

» More about Leodanis

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

MasterReal-World Python Skills With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

MasterReal-World Python Skills
With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students.Get tips for asking good questions andget answers to common questions in our support portal.


Looking for a real-time conversation? Visit theReal Python Community Chat or join the next“Office Hours” Live Q&A Session. Happy Pythoning!

Keep Learning

Related Topics:basicspython

Related Courses:

Related Tutorials:

Keep reading Real Python by creating a free account or signing in:

Already have an account?Sign-In

Almost there! Complete this form and click the button below to gain instant access:

Python Logo

5 Thoughts On Python Mastery

🔒 No spam. We take your privacy seriously.


[8]ページ先頭

©2009-2026 Movatter.jp