0

How to replace the even number in list with any alphabet and print the list using python 3

list = [1,2,3,4,5,6,7,8,9] my output is : 1,a,3,a,5,a,7,a,9 expected output: [1,a,3,a,5,a,7,a,9]

29th Jul 2021, 7:30 AM
Navneet
Navneet - avatar
4 Réponses
+ 2
#way 1: l2 = [i if i%2!=0 else "a" for i in l] print(l2) #way 2: l3 =[] for i in l: if i%2!=0: l3+=[i] else: l3+=["a"] print(l3)
29th Jul 2021, 9:46 AM
Shadoff
Shadoff - avatar
+ 1
l = [1,2,3,4,5,6,7,8,9] for i in range(len(l)): if l[i]%2==0: l[i] = 'a' print(l)
29th Jul 2021, 9:37 AM
RKK
RKK - avatar
0
looks fine. Where's your code?
29th Jul 2021, 7:35 AM
RKK
RKK - avatar
0
list = [1,2,3,4,5,6,7,8,9] for i in list: if i%2==0: print('a') else: print(i)
29th Jul 2021, 9:25 AM
Navneet
Navneet - avatar