We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
List comprehension is a nested loop . we can create this in two ways
the first way
a = int(input())
b = int(input())
c = int(input())
n = int(input())
listt = []
for i in range(a+1):
for j in range(b+1):
for k in range(c+1):
if i+j+k != n:
listt.append([i,j,k])
print(listt)
the second way
a = int(input())
b = int(input())
c = int(input())
n = int(input())
listt = [[i,j,k] for i in range(a+1)for j in range(b+1)for k in range(c+1)if (i+j+k) !=n]
print(listt)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
List Comprehensions
You are viewing a single comment's thread. Return to all comments →
List comprehension is a nested loop . we can create this in two ways
the first way
a = int(input()) b = int(input()) c = int(input()) n = int(input()) listt = [] for i in range(a+1): for j in range(b+1): for k in range(c+1): if i+j+k != n: listt.append([i,j,k])
print(listt)
the second way
a = int(input()) b = int(input()) c = int(input()) n = int(input()) listt = [[i,j,k] for i in range(a+1)for j in range(b+1)for k in range(c+1)if (i+j+k) !=n]
print(listt)