Python Language Loops Looping and Unpacking

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

If you want to loop over a list of tuples for example:

collection = [('a', 'b', 'c'), ('x', 'y', 'z'), ('1', '2', '3')]

instead of doing something like this:

for item in collection:
    i1 = item[0]
    i2 = item[1]
    i3 = item[2]
    # logic

or something like this:

for item in collection:
    i1, i2, i3 = item
    # logic

You can simply do this:

for i1, i2, i3 in collection:
    # logic

This will also work for most types of iterables, not just tuples.



Got any Python Language Question?