Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Adding element to list while iterating

I know that it is not allowed to remove elements while iterating a list, but is it allowed to add elements to a python list while iterating. Here is an example:

    for a in myarr:       if somecond(a):           myarr.append(newObj()) 

I have tried this in my code and it seems to work fine, however I don't know if it's because I am just lucky and that it will break at some point in the future?

EDIT: I prefer not to copy the list since "myarr" is huge, and therefore it would be too slow. Also I need to check the appended objects with "somecond()".

EDIT: At some point "somecond(a)" will be false, so there can not be an infinite loop.

EDIT: Someone asked about the "somecond()" function. Each object in myarr has a size, and each time "somecond(a)" is true and a new object is appended to the list, the new object will have a size smaller than a. "somecond()" has an epsilon for how small objects can be and if they are too small it will return "false"

like image 888
WesDec Avatar asked Sep 20 '10 14:09

WesDec


People also ask

Can you add to list while iterating Python?

Use the Python List append() method to append elements to a list while iterating over the list using a for-in loop. If you append a new element to the list while iterating the list results in a list containing the original list's elements and the new elements.

Can we add element in list while iterating?

You can't modify a Collection while iterating over it using an Iterator , except for Iterator. remove() . This will work except when the list starts iteration empty, in which case there will be no previous element. If that's a problem, you'll have to maintain a flag of some sort to indicate this edge case.

How do I add values to a list in a for loop?

Use the append() method to add elements to a list while iterating over the list. Means you can add item in the list using loop and append method.

Can we modify list while iterating Python?

The general rule of thumb is that you don't modify a collection/array/list while iterating over it. Use a secondary list to store the items you want to act upon and execute that logic in a loop after your initial loop.


2 Answers

Why don't you just do it the idiomatic C way? This ought to be bullet-proof, but it won't be fast. I'm pretty sure indexing into a list in Python walks the linked list, so this is a "Shlemiel the Painter" algorithm. But I tend not to worry about optimization until it becomes clear that a particular section of code is really a problem. First make it work; then worry about making it fast, if necessary.

If you want to iterate over all the elements:

i = 0   while i < len(some_list):     more_elements = do_something_with(some_list[i])     some_list.extend(more_elements)     i += 1   

If you only want to iterate over the elements that were originally in the list:

i = 0   original_len = len(some_list)   while i < original_len:     more_elements = do_something_with(some_list[i])     some_list.extend(more_elements)     i += 1 
like image 168
Johnny Avatar answered Sep 20 '22 08:09

Johnny


well, according to http://docs.python.org/tutorial/controlflow.html

It is not safe to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy.

like image 26
Rohan Monga Avatar answered Sep 20 '22 08:09

Rohan Monga