Iterating over multiple lists (a newbie question)

Victor Muslin victor at prodigy.net
Thu Jan 4 14:55:43 EST 2001


Thank you all for the insights. Here is another variation on the them,
though this involves a single list.

Suppose I want to parse command line arguments (never mind getops,
since this could be some other situation not involving command line
arguments). Here is how I could possibly parse command line arguments
for command: "myprog -port 10 -debug":

	import sys
	debug = port = 0
	i = 1                                      # ugly
	while i < len(sys.argv):
	    if sys.argv[i] == '-debug':
	        debug=1
	    elif sys.argv[i] == '-port':
	        i = i + 1                          # ugly!
	        if i < len(sys.argv):           # very ugly!
	            port = int(sys.argv[i])
	    i = i + 1                              # quite ugly

Pretty ugly, huh? Is there a more elegant way?

On Wed, 03 Jan 2001 21:59:37 GMT, victor at prodigy.net (Victor Muslin)
wrote:

>This may be rather silly, but I can't think of a clever way...
>
>To traverse a single list the code is:
>
>	for item in list:
>		print item
>
>However, if there are multiple lists (of the same length) is there a
>cleverer way to do the following:
>
>	for i in range(0,len(list1)):
>		print list1[i], list2[i]
>
>I would like something like this (which obviously does not work):
>
>	for one,two in list1, list2:
>		print one,two
>
>TIA




More information about the Python-list mailing list