Sean 'Shaleh' Perry shalehperry at
Thu Oct 10 02:04:49 CEST 2002

On Wednesday 09 October 2002 16:58, gabor wrote:
> hi,
> 1.
> a = [1,2,3,4,5,6]
> for item in a:
> 	print a
> will print 1 2 3 4 5 6?
> in other words, is it guarantied that for a list the elements will be
> enumerated in their order?

that is the whole point of a list.  Whatever order you put the items in is the 
order they stay in.  It is a collection with its order based on creation and 
possibly by later manipulation (sorting, etc)

> 2. many times i use this construct:
> a = ["a","b","c","d"]
> for letter in a:
> 	dosomething..
> 	but now i really need the position of the letter in that list
> 	so i need an x, for which a[x] = letter...
> the only solution i could find:
> for index in range(len(a)):
> 	letter = a[index]
> is there a way to do that with the normal 'for letter in a' approach?

a common solution is to use range:

for i in range(0, len(a)):
    print a[i]

there is also the list's index method:

index = a.index('d')
print index

will print '3' and a[3] will print 'd'.

Hope this helps.

Note there is a great tutor at list which is aimed at new pythoners 
(and new programmers in general).

More information about the Python-list mailing list