Ad

suppose list is

l = [1,2,3,4,[1,2,3],(1,2),'a']

and you want to print all elements separated by space

output = 1 2 3 4 [1, 2, 3] (1, 2) a

How can you do that ?

l  = [1,2,3,4,[1,2,3],(1,2),'a']

print(*l)
Lists
Data Structures
Fundamentals
Arrays
Data Types

consider the following code !

l = [1, 2, 3, 4, 5]
a = []

for values in l:
if values==1:
a.append('yes')
elif values==2:
a.appned('no')
else:
a.append('idle')

print(a)

output : ['yes', 'no', 'idle', 'idle', 'idle']

How can you apply elif in list comprehenion ?
And what are the other ways to do this ?

l = [1, 2, 3, 4, 5]
a = ['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]
print(a)

suppose there is list like
l = [[-44, 68], [-4, 108], [0, 112], [-48, 64]]

How can you extract all elements in a single list ?

answer is :
answer = [i for j in l for i in j]
print(asnwer)

output : [-44, 68, -4, 108, 0, 112, -48, 64]

l = [[-44, 68], [-4, 108], [0, 112], [-48, 64]]
answer = [i for j in l for i in j]
print(answer)

Suppose you want to generate a list like below

[1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18]

you can observe pattern that its [i, i*2 for i in range(1,10)]

1 and 1 * 2 = 2
2 and 2 * 2 = 4 ... 9 and 9 * 2 = 18

But we can not execute [i, i*2 for i in range(1,10)]

so very simple solution is

a = []
for i in range(1,10):
a.append(i)
a.append(i*2)

but how can you do that in one line ?

lst_gen = list(sum([(i, i*2) for i in range(1, 10)],()))

print(lst_gen)