How to print [odd,even,odd,even,odd] for 1 to 5 number using list comprehension in python | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 4

How to print [odd,even,odd,even,odd] for 1 to 5 number using list comprehension in python

Above output may be printed using for loop but I want to my output using only list comprehension (must include if else list comprehension) not other methods

9th Jun 2020, 10:19 AM
Runtime Terror
Runtime Terror - avatar
7 Antworten
+ 9
a=["odd" if i%1==0 else "even" for i in range(1,6)]
9th Jun 2020, 10:24 AM
Abhay
Abhay - avatar
+ 6
But I assumed he knew how do list comprehension ,but isn't aware of how to add if else in list comprehension ,so I gave the code
9th Jun 2020, 11:11 AM
Abhay
Abhay - avatar
+ 4
Yup!
9th Jun 2020, 11:13 AM
Abhay
Abhay - avatar
+ 4
Thanks everyone well explained I understand easily
9th Jun 2020, 12:04 PM
Runtime Terror
Runtime Terror - avatar
+ 3
Your attempt? You won't get ready-made code, show your code/attempt we will fix together.
9th Jun 2020, 10:25 AM
Rohit
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Abhay No problem guys 😊 At the end of the day if he has learnt something new, then we should be happy.
9th Jun 2020, 11:12 AM
Rohit
+ 2
Yeah 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 you too are right because struggling and thinking a bit makes us clever and a good problem solver.
9th Jun 2020, 11:13 AM
Rohit