0

I just want to add every entry of a list in lowercase by using lower(). I am using this code to do the task:

MyList = ["EntryOne", "EntryTwo"]
TempList = MyList     #cloning MyList to TempList

for v in TempList:
    MyList.append(v.lower())     #Why is it also being appended into TempList ?
    print(MyList)
    print(TempList)




#expected output:
#["EntryOne", "EntryTwo", "entryone", "entrytwo"]
#["EntryOne", "EntryTwo"]

As you can see, the declaration of TempList is outside of the for loop, i am just declaring it at the beginning. There is no code in which Im appending the lowercase into TempList.

As a result this script loops forever.

  • Please look in to this https://stackoverflow.com/questions/8744113/python-list-by-value-not-by-reference – Ritwik G Apr 12 '21 at 16:13
  • *Nowhere* did you clone a list. `TempList = MyList` **never** copies anything. That simply assigns the *same list object* to another variable. – juanpa.arrivillaga Apr 12 '21 at 16:22

1 Answers1

1

Your mistake is your statement in line #2:

TempList = MyList     #cloning MyList to TempList

This is incorrect. It does not clone your list.

Use .copy() instead:

TempList = MyList.copy()
Finomnis
  • 18,094
  • 1
  • 20
  • 27