Question about order in lists

Thorsten Zöller tzoeller at t-online.de
Fri Aug 13 16:29:50 EDT 1999


Suppose you write a code as follows:

x = []                                                # empty list
list1 = [5, 3, 7]                                  # list containing any
numbers
list2 = [8, 6, 3]                                  # another list containing
any numbers
for i in range(len(list1)):
    x.append(list1[i-1] + list2[i-1])       # [5+8, 3+6, 7+3]
...
x                                                        # print resulting
list

I'm trying to achieve that, having two lists of the same lenght containig
any numbers, each component of the two lists is added to his pendant of the
other lists (i.e. the first component of list1 is added to the first
component of list2 etc). The result is saved in another list x. But if I run
this code, I do not get [13, 9, 19] for x, but [10, 13, 9], i.e. the
components are in a wrong order now. Why does the order change and how can I
avoid it?

Thank you
Thorsten






More information about the Python-list mailing list