Python Language Looping and Unpacking


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.