Dictionary problem

Stian Søiland stain at stud.ntnu.no
Sat Nov 22 15:11:23 EST 2003


* anton muhin spake thusly:

>      for i in range(10):
>          for setting in myList[i]:

Btw, this kind of code I see too much of from fresh python programmers.
Remember that for-loops iterates over sequences by default, so you
really don't need those indexes. In most cases, your code could be
simplified to

       for item in myList:
           for setting in item:

If you really need both items and their indexes, try using zip from
python 2.2:

>>> items = ["a","b","c","d"]
>>> zip(range(len(items)), items)
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]

>>> for (index, item) in zip(range(len(items)), items):
...   print index, item
...
0 a
1 b
2 c
3 d

-- 
Stian Søiland               Being able to break security doesn't make
Trondheim, Norway           you a hacker more than being able to hotwire
http://stain.portveien.to/  cars makes you an automotive engineer. [ESR]




More information about the Python-list mailing list