-1

I was wondering if there was any way to automatically replace certain elements in a list by other ones, without using an if..else statement for each element ? Something like this :

# before :
aL = ['a', 'b', 'c']
# after :
aL = ['b', 'c', 'd']

1 Answers1

1

One way to do it,

aL = ['a', 'b', 'c','z']
expected = []
for ch in aL:
    if ch == 'z':
        expected.append(chr(ord(ch)-25))
    else:
        expected.append(chr(ord(ch) + 1))
print(expected)

DEMO: https://rextester.com/LVY19713

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103