Question about order in lists

Darrell news at dorb.com
Fri Aug 13 16:59:00 EDT 1999


Worked for me.

>>> l1=[5,3,7]
>>> l2=[8,6,3]
>>> x=[]
>>> for i in range(len(l1)):
...     x.append(l1[i]+l2[i])
...
>>> x
[13, 9, 10]

--
--Darrell

Thorsten Zöller <tzoeller at t-online.de> wrote in message
news:7p1v28$vcf$1 at news04.btx.dtag.de...
> 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