-2

For example:

l = ["a", "l", "a", "m", "a", "n", "t"]

In this case I want to change every "a" in the list to "e", like this:

l = ["e", "l", "e", "m", "e", "n", "t"]

I tried indexing and stuff, but it doesn't seem to work.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Polc44
  • 1

1 Answers1

0

You can do this with a list comprehension.

In [1]: l = ["a", "l", "a", "m", "a", "n", "t"]

In [2]: d = {"a": "e"}

In [3]: l = [d.get(i, i) for i in l]

Output:

Out[4]: ['e', 'l', 'e', 'm', 'e', 'n', 't']
Rahul K P
  • 15,740
  • 4
  • 35
  • 52