Header Ads

Responsive Ads Here

List Comprehension

List Comprehension



# l = []

# for i in range(1,11):
#     l.append(i**3)
# print(l)

# x = [i**3 for i in range(1,11)]
# print(x)

# n = ["Mango", "Apple", "Guava"]
# n1 = []

# for i in n:
#     n1.append(i[0])
# print(n1)


# n2 = [i[0] for i in n]
# print(n2)


# l = list(range(1,11))
# even = []

# for i in l:
#     if i%2 == 0:
#         even.append(i)
# print(even)

# even1 = [i for i in l if i%2 == 0]
# print(even1)

# l = list(range(1,11))
# x = []

# for i in l:
#     if i % 2 == 0:
#         x.append(i)
#     else:
#         x.append("O")
# print(x)

# x1 = [i if (i % 2 == 0) else "O" for i in l]
# print(x1)


a = [[1,2,3],[4,5,6],[7,8,9]]
b = []

for j in a:
    for i in j:
        b.append(i)
print(b)

b1 = [i for j in a for i in j]
print(b1)

Powered by Blogger.