1

This is for python and the for loop! I am aware you can use other iterations, I would like to know if there is a solution to this problem using the FOR loop. I apologise if this a duplicate, I did not know how to phrase the problem to search for it. It is a simple question. I like for loops and use them often, but when it comes time to alter the elements as they are iterated, I always seem to have to create a new array with the edited elements. Here is an example:

for item in list:
     if item > 7:
          item += 1

the item in this case is not returned to the list. I find that I have do something like this:

newlist = []
    for item in list:
         if item > 7:
             item += 1
        newlist.append(item)

It is not a huge problem, but each time I write out code similar to this I wonder if there is not a simpler way, and one that doesn't create new variables.

The example above uses built in variable names, it is only an example, and not how anyone should implement a for loop or declare variables

2
  • 4
    don't use keywords as variable names. Commented Mar 6, 2015 at 9:20
  • 2
    And don't use the names of built-in types like list, str, int', dict`, etc, as variable names, either. This recent question is an example of what can happen if you don't follow this rule. Commented Mar 6, 2015 at 9:30

2 Answers 2

5

One of the things you can do

for i, item in enumerate(my_list):
  if item > 7:
    my_list[i] += 2
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a generator expression or a list comprehension to change the original list:

lst = [1,3,5,7,8,9,10]

lst[:] = (i + 1 if i > 7 else i for i in lst)

print(lst)
[1, 3, 5, 7, 9, 10, 11]

Using a list comp:

 lst[:] = [i + 1 if i > 7 else i for i in lst]

The [:] syntax changes the original list.

It is also more efficient:

In [18]: %%timeit
my_list = list(range(10000))
for i, item in enumerate(my_list):
  if item > 7:
    my_list[i] += 1
   ....: 
100 loops, best of 3: 2.04 ms per loop

In [19]: %%timeit
lst = list(range(10000))
lst[:] = (i + 1 if i > 7 else i for i in lst)
   ....: 

1000 loops, best of 3: 1.55 ms per loop

In [20]: %%timeit
lst = list(range(10000))
lst[:] = [i + 1 if i > 7 else i for i in lst]
   ....: 
1000 loops, best of 3: 1.28 ms per loop

3 Comments

That works thanks. I have a follow up question, when you say more efficient, you are referring to the use of resources to complete the task right?
@DaleLudwig, list comprehensions are optimised, a special LIST_APPEND bytecode is generated so we avoid the overhead of calling the list.append method.
There is some interesting performance enhancing ideas here wiki.python.org/moin/PythonSpeed/PerformanceTips

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.