0

I have a very long list like this:

old_list = ['cat','dog','bat','cat',...]

I am using the following code to copy this entire list into another with changes for some specific conditions:

new_list = []
for item in old_list:
    if item == ("cat" or "bat"):
        new_list.append("a")
    elif item == "dog" or "fog" or "sog" or "log":
        new_list.append("o")
    else:
        new_list.append(item)

The problem is that in some cases "cat" and "bat" are correctly replaced by "a" but in some cases "cat" or "bat" remains as it is when I print new_list

What am I doing wrong?

2 Answers2

2

use

if item in {"dog", "fog", "sog", "log"}:
    ...
Finlay McWalter
  • 1,222
  • 1
  • 10
  • 12
1

This will solve your isse

old_list = ['cat','dog','bat','cat']
new_list = []
for item in old_list:
    if item == "cat" or item=="bat":
        new_list.append("a")
    elif item == "dog" or item== "fog" or item=="sog" or item=="log":
        new_list.append("o")
    else:
        new_list.append(item)
print(new_list)
ricristian
  • 466
  • 4
  • 17