-
list manipulation with list.pop[i]
Hi everyone,
question: i have a list of points, a list vectors and a list of angles. Say i put all if them in one nested list:
List = []
List[0].append(points)
List[0].append(vectors)
List[0].append(angles)
I then send each nested list to a different definition in order to be evaluated and depending on different if conditions some slots are moved to new lists.
the new lists can be returned from the definition and appended to the original Group as such
List.append(new_list1)
List.append(new_list2)
etc
But at the same time I want to use the command list.pop(i) in order to remove the elements moved from the original List at index [0] because it is important to keep everything in the original "List" slots and only append more on the way (until all elements basically are dropped, List has only empty slots and script ends)
I'm a bit confused though with the List.pop(i). If you start removing elements of a list at 'i' location during an 'for i in List' loop won't that confuse the loop since the list would be getting smaller all the time and then the index will get out of range?
any suggestions on how to work through this welcome
regards,
Pav
-
Senior Member
Hi pavlos03
lista=[]
lista.append(points)
lista.append(vectors)
lista.append(angles)
is the right syntax
Ciao Vittorio
-
Hi Vittorio,
yes that's the correct syntax, my mistake, still on vbscript mode.
but still my question is further down in the thread, regarding the index.pop(i) command, if you could check it out.
thanks
Pav
-
Hi pavlos03,
Doing list.pop(i) while iterating with index over a list ist not a good idea. can be very confusing.
You could replace an item in your list with "None" instead of Pop(). then your indices stay correct.
Are you aware that python list are always passed by reference?
That means if you pass a list to a function and change it there the original list wil change too.
does this help ?
-
Member
Also, as a sort of side note:
list.pop(i) actually returns the element with the index i. If you simply want to permanently delete/remove an item from a list you could use the del statement: http://docs.python.org/tutorial/data...-del-statement
But as Goswin says, be careful when doing these kind of things.
Last edited by AHD; 10-09-2012 at 09:34 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules