Mailing List Archive

Question about order in lists
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
Question about order in lists [ In reply to ]
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@t-online.de> wrote in message
news:7p1v28$vcf$1@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
>
>
Question about order in lists [ In reply to ]
"Thorsten Zöller" <tzoeller@t-online.de> writes:
| 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?

Because you index the input lists with i-1. Use list1[i] + list2[i],
and then I think it will work as you want.

range() naturally returns a list that starts with 0, which happens to be
where Python sequences start as well. When you decrement that initial 0
index, -1 is like len(list1)-1, hence the last shall be first.

Donn Cave, University Computing Services, University of Washington
donn@u.washington.edu
Question about order in lists [ In reply to ]
Or even:

>>> import operator
>>> l1=[5,3,7]
>>> l2=[8,6,3]
>>> map( operator.add, l1,l2)
[13, 9, 10]
>>>

Enjoy,
Mike

-----Original Message-----
From: python-list-request@cwi.nl [mailto:python-list-request@cwi.nl]On
Behalf Of Darrell
Sent: August 13, 1999 4:59 PM
To: python-list@cwi.nl
Subject: Re: Question about order in lists


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@t-online.de> wrote in message
news:7p1v28$vcf$1@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
>
>
Question about order in lists [ In reply to ]
Thorsten Z÷ller asks:

> 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?

Interactive is your friend!

>>> x = []
>>> list1 = [5,3,7]
>>> list2 = [8,6,3]
>>> for i in range(len(list1)):
... print "i is", i
... print "i-1 is", i-1
... print "list1[i-1] is", list1[i-1]
... print "list2[i-1] is", list2[i-1]
... x.append(list1[i] + list2[i]) #oops, that's the right way!
...
i is 0
i-1 is -1
list1[i-1] is 7
list2[i-1] is 3
i is 1
i-1 is 0
list1[i-1] is 5
list2[i-1] is 8
i is 2
i-1 is 1
list1[i-1] is 3
list2[i-1] is 6
>>> x
[13, 9, 10]
>>>

- Gordon
Question about order in lists [ In reply to ]
You win.
Map is very fast in this case.
Although something intresting happend when I used
l1[:] to create l2
##################
import operator, time

def mapIt(l1, l2):
map( operator.add, l1,l2)


def loopIt(l1, l2):
x=[]
append=x.append
for i in range(len(l1)):
append(l1[i]+l2[i])


sz=1000000
l1=[]
for i in xrange(sz):l1.append(i)

if 0:
# Time1: 5.75 sec
# Time2: 27.70 sec
l2=[]
for i in xrange(sz):l2.append(i)
else:
# Odd when using these huge numbers
# This way of creating l2 ran faster ??
# Time1: 3.25 sec
# Time2: 28.50 sec
l2=l1[:]

t1=time.time()
mapIt(l1,l2)
print " Time1: %.2f sec"%(time.time()-t1)
t1=time.time()
loopIt(l1,l2)
print " Time2: %.2f sec"%(time.time()-t1)

####################
# Time1: 3.25 sec
# Time2: 28.50 sec



--Darrell
Question about order in lists [ In reply to ]
Understandable behaviour:

l2 = l1[:] -> make a new list with size == l1 with pointers all pointing to
l1's data.

At the C level, this would mean approximately "copy memory range x to new
memory range y" (lists are contiguous arrays of pointers to objects).

Whereas:

for i in xrange(sz):l2.append(i)

Actually goes through x python loop iterations, where it binds the variable
i x times, finds attribute __getitem__ of your xrange x times, calls it x
times, finds the attribute append of l2 x times, and calls that function x
times. (And every single variable lookup has the potential to fall through
to a higher-level namespace etceteras). Python's dynamism has a _serious_
impact on performance and algo design ;) .

Oh, and the following is a fairly fast way to create your initial list of
integers (in this case, just a little faster):

range( 1000000 )

Cheers,
Mike

-----Original Message-----
From: python-list-request@cwi.nl [mailto:python-list-request@cwi.nl]On
Behalf Of Darrell
Sent: August 13, 1999 7:06 PM
To: python-list@cwi.nl
Subject: Re: Question about order in lists


You win.
Map is very fast in this case.
Although something intresting happend when I used
l1[:] to create l2
##################
import operator, time

def mapIt(l1, l2):
map( operator.add, l1,l2)


def loopIt(l1, l2):
x=[]
append=x.append
for i in range(len(l1)):
append(l1[i]+l2[i])


sz=1000000
l1=[]
for i in xrange(sz):l1.append(i)

if 0:
# Time1: 5.75 sec
# Time2: 27.70 sec
l2=[]
for i in xrange(sz):l2.append(i)
else:
# Odd when using these huge numbers
# This way of creating l2 ran faster ??
# Time1: 3.25 sec
# Time2: 28.50 sec
l2=l1[:]

t1=time.time()
mapIt(l1,l2)
print " Time1: %.2f sec"%(time.time()-t1)
t1=time.time()
loopIt(l1,l2)
print " Time2: %.2f sec"%(time.time()-t1)

####################
# Time1: 3.25 sec
# Time2: 28.50 sec



--Darrell