0

In a for-loop I attempt to overwrite string-type variables.

item1 = "Item 1"
item2 = "Item 2"
for item in [item1, item2]:
    if item == "Item 2":
        item = "Item 1"
print (item1, item2)

The print that results says "Item 1 Item 2". It should say "Item 1 Item 1"

I also tried item = item.replace("Item 2","Item 1"). Same result.

What prevents "Item 2" from getting replaced?

Update:

Similar to Changing iteration variable inside for loop in Python but with strings, not integers.

I have a much longer list of variables to validate and overwrite, so a for-loop that just uses the current item for reassignment would be ideal (as opposed to item2 = "Item 1")

12
  • Possible duplicate of stackoverflow.com/questions/15363138/… btw you cannot change value of loop variable which is elaborated in the link. Commented Mar 24, 2017 at 15:27
  • 1
    I'm not sure why you think your loop should do what you think it should do. You never reassign item1 or item2, so why are you confused that they have not changed? Commented Mar 24, 2017 at 15:27
  • 1
    Dup of stackoverflow.com/questions/4081217/…. Commented Mar 24, 2017 at 15:29
  • 1
    Guys, this is not a duplicate of a "change list while iterating over it" question. The central point here is that OP does not understand how assignment works. Commented Mar 24, 2017 at 15:30
  • 1
    To be more precise, you are tripping over the fact that "Names are reassigned independently of other names." nedbatchelder.com/text/names.html Commented Mar 24, 2017 at 15:41

1 Answer 1

4

You're re-assigning the temporary variable item which is assigned on every iteration of the for-loop. So basically, you re-assign item to "Item 1" and then the interpreter immediately re-assigns it again on the next iteration to "Item 2". In any case, however, you are never mutating the original list by re-assigning this variable.

If you really want the last line to print what you want, then you want to re-assign your original variables instead:

item1 = "Item 1"
item2 = "Item 2"
for item in [item1, item2]:
    if item == "Item 2":
        item2 = "Item 1"
print (item1, item2)

It makes more sense however to make a list of the changes, though. This is the common pattern:

old_list = ["Item 1", "Item 2"]
new_list = []
for item in old_list:
    if item == "Item 2":
        new_list.append("Item 1")
    else:
        new_list.append(item)

print new_list
Sign up to request clarification or add additional context in comments.

2 Comments

There's a builtin for this "map"
It's a style choice, for something this simple. I personally consider defining a new function or writing a lambda function here as less elegant. If anything, a list comprehension might be the next step here, but with this example even that's pretty weird.

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.