[Tutor] iterating

Cecilia Alm ebbaalm at uiuc.edu
Mon Apr 30 15:42:34 CEST 2007


1) Unordered maps/collections like sets and dictionaries also seem to
support iterating over set members or dictionary keys with "for x in
y" syntax. As far as I can tell, the following three ways generally
behave the same way, or is there a difference in behavior between:

a) for key in dictionary:
b) for key in dictionary.keys():
c)  mykeys = dictionary.keys()
     for k in mykeys:

2) The following three ways for manipulating objects in a list with a
function will generally do the same thing, right? (but the last two
have a shorter syntax).

a)  newls = []
     for entry in mylist:
        newls.append(function(entry))

b)  newlist = map(function, mylist)

c)  newlist = [function(entry) for entry in mylist]


More information about the Tutor mailing list