Home 08. List Comprehensions
Post
Cancel

08. List Comprehensions

Problem: List Comprehensions

Solution 1: Using for loop and if-else statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
x = int(input())
y = int(input())
z = int(input())
n = int(input())

arr = []

for i in range(x + 1):
    for j in range(y + 1):
        for k in range(z + 1):
            if i + j + k != n:
                arr.append([i, j, k])

print(arr)

Solution 2: Using for loop and if-else statement and one-liner

1
2
3
4
5
6
x = int(input())
y = int(input())
z = int(input())
n = int(input())

print([[i, j, k] for i in range(x + 1) for j in range(y + 1) for k in range(z + 1) if i + j + k != n])
This post is licensed under CC BY 4.0 by the author.

Welcome | Irfan's Blog

A