can someone remind me of the difference between append and extend ?
let say with a vertex vector
pt = [xi,yi,zi]
verts1.append(pt)
Thanks
can someone remind me of the difference between append and extend ?
let say with a vertex vector
pt = [xi,yi,zi]
verts1.append(pt)
Thanks
L1= [a,b,c]
L2= [d,e,f]
L1.append(L2) => L1= [a,b,c, [d,e,f]]
L1.extend(L2) => L1= [a,b,c, d,e,f]
I guess I don’t get it.
I have never used extend, what does it do?
ok i think i’m beginning to see the difference here
but sorry the example given is not the right one i should have use !
i began to append to a list
but forgot to zeroed it before re using it
and i ended up with a list where values where repeated at each append
and the list was growing the wrong way with far too many elements then expected
i think i figure out that if i zeroed my list before re using it would settle the problem
and that seems to work fine now
difference
the difference is not easy to see but it has to do with how the elements of a list are added
to an existing list
the append is adding whatever other list’s elements to the existing list
and you get a list of list
but the extend will simply add the element of watever is added to the existing list
so you end up with a longer list of elements and not a list of lists
hope it’s clearer at least i better understand it now
Flat list
now is the extend giving what can be called a flat list ?
or is a flat list like a float sequence and not a list ?
Atom ?
extend is used with Curve i think not with list of vertex , edge and faces
Thanks
here the script i did to test theses differences
import bpy
#import Mathutils Change to mathutil Mars 2010
import mathutils
from math import *
from bpy.props import *
import random
print ()
print (' #################### Begin ##########')
print ()
verts1=[]
pt = [1,2,3]
verts1.append(pt)
pt = [4,5,6]
verts1.append(pt)
pt = [7,8,9]
verts1.append(pt)
print ('Append verts1=',verts1,' Lenght =',len(verts1))
print ()
#################################
vert=[]
pt = [1,2,3]
vert.extend(pt)
pt = [4,5,6]
vert.extend(pt)
pt = [7,8,9]
vert.extend(pt)
print ('Extend vert=',vert,' Lenght =',len(vert))
print ()
vert2=[]
vert2.extend(verts1)
vert2.extend(vert)
print ('verts1=',verts1)
print ('vert=',vert)
print ('Extend Verts1 and verts = vert2=',vert2,' Lenght =',len(vert2))
print ()
vert3=[]
vert3.append(verts1)
vert3.append(vert)
print ('verts1=',verts1)
print ('vert=',vert)
print ('Append Verts1 and verts = vert3=',vert3,' Lenght =',len(vert3))
print ()
a=10
b=11
c=12
d=20
e=21
f=22
vert4=[]
float1=(a,b,c)
float2=(d,e,f)
vert4.append(float1)
vert4.append(float2)
print ('Append float1 and float2 = vert4=',vert4,' Lenght =',len(vert4))
print ()
vert5=[]
vert5.extend(float1)
vert5.extend(float2)
print ('Extend float1 and float2 = vert5=',vert5,' Lenght =',len(vert5))
print ()
print (' Extend means that you have only one list of items added form lists or tuples ')
print (' #################### END ############')
print ()
L1= [a,b,c]
L2= [d,e,f]
‘Append’ will add L2 as L1 element:
L1.append(L2) => L1= [a, b, c, [d, e, f]]
‘Extend’ will add L2 elements to L1:
L1.extend(L2) => L1= [a, b, c, d, e, f]
i think you got it wrong here
try my little script
append make a lilst of list not item list + list
like this
#################### Begin ##########
ppend verts1= [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Lenght = 3
xtend vert= [1, 2, 3, 4, 5, 6, 7, 8, 9] Lenght = 9
erts1= [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ert= [1, 2, 3, 4, 5, 6, 7, 8, 9]
xtend Verts1 and verts = vert2= [[1, 2, 3], [4, 5, 6], [7, 8, 9], 1, 2, 3,
5, 6, 7, 8, 9] Lenght = 12
erts1= [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ert= [1, 2, 3, 4, 5, 6, 7, 8, 9]
ppend Verts1 and verts = vert3= [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [1, 2,
4, 5, 6, 7, 8, 9]] Lenght = 2
ppend float1 and float2 = vert4= [(10, 11, 12), (20, 21, 22)] Lenght = 2
xtend float1 and float2 = vert5= [10, 11, 12, 20, 21, 22] Lenght = 6
try my little script
what about the tuple and flat list ?
Thanks
i think you got it wrong here
Where am I wrong?
try my little script
Your script is better than python docs?
append make a lilst of list not item list + list
Look below:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> l_1= [1, 2, 3]
>>> l_2= [4, 5, 6]
>>> l_1
[1, 2, 3]
>>> l_1.append(l_2)
>>> l_1
[1, 2, 3, [4, 5, 6]]
>>> len(l_1)
4
>>> l_3= [7,8,9]
>>> l_2.extend(l_3)
>>> l_2
[4, 5, 6, 7, 8, 9]
>>> len(l_2)
6
>>> l_1.append(l_3)
>>> l_1
[1, 2, 3, [4, 5, 6, 7, 8, 9], [7, 8, 9]]
>>> len(l_1)
5
>>> l= [1,2,3]
>>> p=(1,2,3)
>>> l.append(p)
>>> l
[1, 2, 3, (1, 2, 3)]
>>> k=('a','b')
>>> l.extend(k)
>>> l
[1, 2, 3, (1, 2, 3), 'a', 'b']
>>> len(l)
6
>>>
this one i think is wrong
L1.append(L2) => L1= [a, b, c, [d, e, f]]
should be like
= [ [a, b, c, ] , [d, e, f] ]
it should give a list of list
like my first example
ppend verts1= [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Lenght = 3
salutations
this one i think is wrong
No, your is wrong. And you miss one thing - initial state.
like my first example
ppend verts1= [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Lenght = 3
This because your initial state is .
Than you append another list [a,b,c] => this leads to [[a,b,c]]
Than you append another list [d,e,f] => this lead to [[a,b,c], [d,e,f]]
In my example initial state is [a,b,c] and when I append another list [d,e,f] this goes to => [a,b,c,[d,e,f]]
and if I extend the list, this will go to [a,b,c,d,e,f]
Check console log which I post above for details.
ok sorry - it’s a difficult concept to master
and have to be carefull where you use it what you gone get !
like initial l1=[1,2,3] this is a simple
but mine began with a list of list
then did the append
so got tob e carefull how you begin it !
but what about this flat list thing
any ideas on this ?
Thanks
@RickyBlender, googling is good =) http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
still need to have the proper search keywords
i tried so many times and failed to find anything good
the forum here is for python so should be the place to find answers
for theses flat list now i got to make a new test script for theses
but have your ever tested this
$ python -mtimeit -s’l=[[1,2,3],[4,5,6], [7], [8,9]]*99’ '[item for sublist in l for item in sublist
10000 loops, best of 3\ 143 usec per loop
timeit is there a special module for this one or use the other one for timer i think?
i need to make a new script for theses flat list
Thanks anyway
which module should i load to use the reduce function?
a=reduce(lambda x,y: x.extend(y) or x, l)
always get error on that reduce function ?
never use this time function before so
when using this
timeit.Timer(
‘sum(l, [])’,
‘l=[[1, 2, 3], [4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7]]’
).timeit()
where does the time is shown ?
how do you print it on the console
Thanks
how do you print it on the console
print()
which module should i load to use the reduce function?
http://diveintopython3.org/porting-code-to-python-3-with-2to3.html#reduce
In Python 3, the reduce() function has been removed from the global namespace and placed in the functools module.
So:
from functools import reduce
reduce(a, b, c)
Try using python3 command line for tests:
┌─(bdancer@bdancer-lucid:pts/0)─────────────────────────────────────────────────(~)─┐
└─(13:34:%)── python3 ──(Sat,Jul03)─┘
Python 3.1.2 (r312:79147, Apr 15 2010, 15:35:48)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from functools import reduce
>>> l=[[1,2,3],[4,5,6], [7], [8,9]]*99
>>> reduce(lambda x,y: x+y,l)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7,etc...]
>>> l=[[1,2,3],[4,5,6], [7], [8,9]]*99
>>> [item for sublist in l for item in sublist]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5,etc]
>>> l
[[1, 2, 3], [4, 5, 6], [7], [8, 9], [1, 2, 3], [4, 5, 6],etc]
>>> [item for sublist in l for item in sublist]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1,etc]
>>> timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'timeit' is not defined
>>> import timeit
>>> timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()
2.728785991668701
>>>
interesting ways to go from a list to a flat list
did not think there was that many ways of doing it !
working in 2.49 have to test this in 2.5 now
i think the timeit is giving the time in seconds but not certain here ?
timeit the only thing that did not work is the example on python doc page
http://docs.python.org/library/timeit.html?highlight=timeit#module-timeit
Last example for timing a function
it might be usefull for testing some functions
#Main
def test():
“Stupid test function”
L = []
for i in range(100):
L.append(i)
if name==‘main’:
from timeit import Timer
t = Timer(“test()”, “from main import test”)
print t.timeit()
where and how do you set this “Main” thing
i get an error on import test
may be there is another module to be imported for that one too!
but in a way i prefer to use the old timer function but it may not be as precise as this one!
at least i can set it anywhere i want and just do the difference t2- t1
Thanks happy 2.5