Hlelp clean up clumpsy code

Pierre Quentel quentel.pierre at wanadoo.fr
Tue Jan 4 16:28:37 EST 2005

You can also do it in a more pythonic way but without generators :

# a = [[1,5,2], 8, 4]
# l = []
# for item in a:
#    if isinstance(item, (int, long)):
#        l.append(item)
#    else:
#        l+=item
# print dict([(item,i+1) for (i,item) in enumerate(l)])

It works in the same conditions as your original code (no nested lists)

A few other things :
- you don't have to type a comma for one-item lists : x = [x] works - 
you probably confused with tuples where you must do x=(x,)
- instead of
# for w in [y for y in x]:
just do
# for w in x:
-  for "i = i+1" there is a shortcut : i+=1 (see "l+=item" above)


More information about the Python-list mailing list