How to iterate over two lists - python

Hey guys. I am trying to do something in pyGTk where I am creating a HBoxes list:

self.keyvalueboxes = []
for keyval in range(1,self.keyvaluelen):
    self.keyvalueboxes.append(gtk.HBox(False, 5))

But then I want to run the list and assign a text entry and label to each of them that are stored in the list.

I'm sorry that I'm not very specific, but if you need more help with what I'm doing, I will help!

Thank!

+3
source share
2 answers

If your list is of equal length, use zip

>>> x = ['a', 'b', 'c', 'd']
>>> y = [1, 2, 3, 4]
>>> z = zip(x,y)
>>> z
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> for l in z: print l[0], l[1]
... 
a 1
b 2
c 3
d 4
>>> 
+4
source

Check out http://docs.python.org/library/functions.html#zip . It allows you to iterate over two lists at the same time.

+1
source

Source: https://habr.com/ru/post/1771829/


All Articles