For Loop With Two Variables Python

I am discovering python and also I have actually checked out some pretty funky for loops iterating multi-dimensional arrays and also k, v values in dictionaries.

You watching: For loop with two variables python

Anyone obtained a attach or explanation to a novice to assist understand?


If you have a list which includes tuples, then you have the right to loop over it commonly, extracting the entire tuple.

my_list = <(1,2,3), (4,5,6), (7,8,9)>for triple in my_list: print(triple)When your list contains lists or tuples, you deserve to declare multiple variables left of the in operator to unload that list or tuple.

for x, y, z in my_list: print(x, y, z)You can carry out the very same via the zip function to produce tuples containing the i'th elements of multiple lists of equal dimension on the fly.

See more: Before We Were Yours Book Review, Before We Were Yours By Lisa Wingate

for x, y, z in zip(range(1,4), range(4,7), range(7,10)): print(x, y, z)See more around Unpacking, I recommend Trey Hunner's blog

Gotta love zip, helps so much!

I recently required to iterate with 2 dictionary k/v pairs at when, it wasn't working till I realized that I needed to use tuples. The original context was a little abstract, so I've captured a nonsense for loop to show my allude. (I fully acquire that tright here are much better ways to execute the following operation.)

for (k1,v1),(k2,v2) in zip(dict1.items(), dict2.items()): print(max(v1,v2))Nonethemuch less, zipping 2 dictionaries together needs 2 tuples. It's a neat trick as soon as you require it!

Note that in for loops "in" is simply a separator and is used to recognize which variables are temporarily bound to the worth obtained from the iterator. It have to not to be puzzled via the membership test operator through the very same name.

I like to describe this in terms of an easy analogy. In python for loop is choose a one-of-a-kind switch to a candy dispenser. You press the switch and also the dispenser gives out candies one by one till they run out (or you decide to break bereason you're obtaining a sugar high). However before the twist is that in the dispenser tbelow give out a single candy or a packet of candy (or packet of candy inside a packet and so on...:)). You have the right to select to obtain the totality packet by pushing a switch OR if you favor you can ask the machine to unwrap the packet and also dispense individual candies. However before you must have that many type of separate containers to hold the unpacked candies, otherwise the machine will throw an error "No candy for you!".

In python human being these candy dispensers are called iterators and they have the right to be lists, tuples, strings, dictionaries and so on. There's some subtlety in what each iterator returns in a for loop (for example using just for key in dict returns the key of a dictionary). But the fundamental principle is the very same, if the iterator retransforms a single worth you can bind that to a solitary variable. If the iterator retransforms one more iterable you have the right to either bind that to a single variable or you have the right to unfill it by making use of multiple variables.

A excellent advantage of this plan is that for instance in a loop if you desire to gain both the index and aspect you have the right to usage enumerate feature which returns a list of tuples containing the element and also its index. For example for i, elem in enumerate(somelist): print(i, elem).