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]
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)
+ 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)
0
looks fine.
Where's your code?
0
list = [1,2,3,4,5,6,7,8,9]
for i in list:
if i%2==0:
print('a')
else:
print(i)