I have the following lists:
mylist = ["FOO", "BAR", "NOTFOO", "NOTBAR"]
mylist2 = ["Foo", "Bar"]
I want to replace words in mylist which exist in mylist2, but as uppercase. my desired output would look like:
mylist = ["Foo", "Bar", "NOTFOO", "NOTBAR"]
I tried the following. However there was no change in mylist.
for word in mylist:
for word2 in mylist2:
if word == word2.upper():
word.replace(word, word2)
In Python's for item in somelist
syntax, item
is essentially set to a copy of each element in the list on each iteration, rather than a reference to anelement in the list.
You also don't really need the inner loop, since Python's in
condition can be used to check for list membership.
And also note it looks like you want the replaced items capitalized, not all-uppercase.
I think a relatively Pythonic way to accomplish what you want is:
for i, word in enumerate(mylist):
if word.capitalize() in mylist2:
mylist[i] = word.capitalize()