num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
odd = []
even = [sum if sum % 2 == 0 else odd[sum] for sum in num]
print(even)
print(odd)
ارور:
Traceback (most recent call last):
File "/home/sadra/Education/python/tempCodeRunnerFile.py", line 3, in <module>
even = [sum if sum % 2 == 0 else odd[sum] for sum in num]
File "/home/sadra/Education/python/tempCodeRunnerFile.py", line 3, in <listcomp>
even = [sum if sum % 2 == 0 else odd[sum] for sum in num]
IndexError: list index out of range
سلام خسته نباشید
برای اینکار از این کد میتونید استفاده کنی
list1 = [11,23,45,23,64,22,11,24]
#list comprehension
even_nos = [num for num in list1 if num % 2 == 0]
print("Even numbers : ", even_nos)
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟