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.

x, y, z, n = int(input()), int(input()), int(input()), int(input())
print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if a + b + c != n ])

range(x,y) will create a range of elements from x to y-1
if only one parameter is passed x is assumed to be 0
so range(y) will create elements in range from 0 till y-1

so here range(4) will create a range which includes 0,1,2,3 hence the loop will iterate 4 times as there are 4 elements

it's because since it has to take 4 inputs
for the first iteration it takes the 'x' input
and for second it takes 'y'
similarly for third it takes "z"
finally foe third it takes "n"

It's also an old Perl trick. If you just performed the input command without assigning the data to anything, it was still available in the $_ variable by default.

It just like a while loop (Ex: while(a<5)) . If we need to store in the seperte varible you can use like this x,y,z,a = (int(input()) for n in range(4). It will be stored seperatly

range() is inclusive of the first number (which is 0, by default), but exclusive of the last one. So "for i in range(5): print i" will give you 0,1,2,3,4 (but not five). In this case, we want to also include the number given, hence the +1. The 0 for the first part range(0,5), is optional, since we are starting at the beginning.

read the question carefully you can understand easily.in the question they have given values inside the nested list must not equal to n so we have given a+b+c!=n

range(4) means range(0,4) that is 0,1,2,3 which is because we ahve to read 4 inputs
Read list comprehensions for more. It is an easy way to build a list.

Makes code less readable, also that doesn't improve time complexity or space complexity so I don't find it that useful other than save some lines of code :S

Can you tell me how python carries out list comprehension internally? How come this generates lists in lexicographic increasing order (i.e z increases first, then y, then x)?

It is in lexographical order due to the nature of the loops.
Spaced out it looks somewhat like

for a in range(x+1):
for b in range(y+1):
for c in range(z+1):
if a + b + c != n:
print(stuff is here)

We start at [0,0,0].
Then c will increment to get to [0,0,1]
When c hits [0,0,z], we get [0,1,0] as the next in the loop. This pattern continues and gives us the lexographical ordering required of the output.

I don't like these "direct print" solutions. In my opinion the problem stated to create a list then print it, not just print something in a list. In other words in your solutions the list itself doesn't exist, can not be used later, it just collected when printed out. What do you think?

OK, I found the answer! I can put list = instead of printoing out, then print the list. It wasn't clear for me three for loops output is collected in a list, so no need for any append tricks!

Could someone explain to me what, exactly, this code does (like, statement-by-statement)(other than the input part I understand that I mostly mean the list comprehension)? I sort of have a basic understanding of how it works, but having some trouble wrapping my head around list comprehensions and how exactly the work. Mostly the "for a in range(0,x+1)" parts. What exactly does that do? Why is it (0,x+1)? Thanks in advance.

Thank you very much bro.....
i am new to python...
initially i din't understand your code but i realized after converting it into normal form.
x, y, z, n = int(input()), int(input()), int(input()), int(input())
for i in range(0,x+1):
for j in range(0, y + 1):
for k in range(0, y + 1):
if i + j + k !=n:
print([i,j,k])

@gkeswani92 - can you please confirm what will be the order of complexity for this code ? is this still considered a nested loop or is this considered linear ?

x, y, z, n = int(input()), int(input()), int(input()), int(input())
print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if x + y+z != n ])
i have written the above code,but it is counting the a+b+c= n entries also..can someone explain why?

Hi Niket,
The problem lies in your if condition, you're running loop with a,b,c but your comparing with it with input limit variables.
Try the below snipped of code I'm sharing and this should work :

x, y, z, n = int(input()), int(input()), int(input()), int(input())

print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if (a + b + c) != n ])

## List Comprehensions

You are viewing a single comment's thread. Return to all comments →

Looks good, but to avoid those repetitive input calls you could do something like:

Thanks for the input. Will use this from now! :D

Please explain for me why you use

`range(4)`

did you got the output for the above one by using list comprehension?

This expression is used for reducing the repetitive statements in reading the inputs from console.

range(x,y) will create a range of elements from x to y-1 if only one parameter is passed x is assumed to be 0 so range(y) will create elements in range from 0 till y-1

so here range(4) will create a range which includes 0,1,2,3 hence the loop will iterate 4 times as there are 4 elements

can you write the complete code snippet using the range(4), I'm getting error. Thanks!

it will take 4 inputs i.e from 0 to 3...

because we have four variables x,y,z,n

because there is 4 inputs ! hope you understand

he want to take input of 4 numbers so in range it counts from 1 then x later until 4 means till n

to get 4 inputs. we are looping the input().

@annguyen bcz its starts from 0 end to 3

it's because since it has to take 4 inputs for the first iteration it takes the 'x' input and for second it takes 'y' similarly for third it takes "z" finally foe third it takes "n"

Great tip! BTW is there any reason for using '_' as the dummy variable? What's wrong with using letter, for example 'i'?

_ is used to signify that even though something is being returned, we don't plan to use that variable any where.

gives the following error in above code in python 2.7.13 : at 0x7f60c947e910>

You need to put an additional [] in addition to [a,b,c]. Because the output that you want is are lists within a list.

It's also an old Perl trick. If you just performed the input command without assigning the data to anything, it was still available in the $_ variable by default.

It just like a while loop (Ex: while(a<5)) . If we need to store in the seperte varible you can use like this x,y,z,a = (int(input()) for n in range(4). It will be stored seperatly

no (lol u w8ted 3yrs 4 answer ;D)

it depends on programmer what to choose , its your wish

just a small suggestion ..the 4 in for loop will be n+1

N is the number to compare the sum of x, y, z. The loop should actually have 4, as mentioned, for the inputs : x,y,z & n.

in py2 can be w/o int()

Nice solution

WHAT IS THE 4 INDICATES HERE

range(4) indicates 0,1,2,3 i.e the numbers we have given are allocated to the x,y,z,n respectively

4 input() s

what is the reason for x+1 y+1 and many more

In simple words it signifies that the range is inclusive of max limit

like the control statement

range() is inclusive of the first number (which is 0, by default), but exclusive of the last one. So "for i in range(5): print i" will give you 0,1,2,3,4 (but not five). In this case, we want to also include the number given, hence the +1. The 0 for the first part range(0,5), is optional, since we are starting at the beginning.

in python iteration occurs one less than the give variable

Can you help me with this logic ? why we check for a+ b +c !=n

read the question carefully you can understand easily.in the question they have given values inside the nested list must not equal to n so we have given a+b+c!=n

why does this code run only for python 2 and not python 3?

Doesn't work on python3.5

int is not required for input()

Yes it is in Python 3

I'm using this one line, but my compiler reads "wrong answer". Am I missing something? What else do I need to write?

u didnt arrange the list in lexicographic order

Disregard, I figured it out.

can u send the solution

I am a newbie to python and its not clear to me. WHY 4? And how the variable iterate from 0 to 1.

range(4) means range(0,4) that is 0,1,2,3 which is because we ahve to read 4 inputs Read list comprehensions for more. It is an easy way to build a list.

How is this syntax even valid. Just wondering.

what does for _ in range(4) do ? what's the significance of the underscore? @omalsa04

I think that _ is used as a dummy variable name. Since it's meaning isn't really significant the name can be something representing that.

Wow nice way to take inputs.

why range (4) ???

why do i need to add parentheses around

in order for this code to work?

coz you have to assign these values to different variables

in place '_' you should 'n'

both methods are coprrect :0

Makes code less readable, also that doesn't improve time complexity or space complexity so I don't find it that useful other than save some lines of code :S

You can also take in the entire buffer at once with

or you could use functions

x, y, z, n = (int(input()) for _ in range(4)) isn't working for me. I'm getting a EOF when reading line error.

@vorayash0510, this is great!

nice solution. But instead of range[0,x] you could do

clever!!!

Trade three +1's for a +1 and a -1 at the right place :P

although this method is more simple, but it not easy to understand.

great!

I liked this method compared to other's.

really like this solution. simple and easy to read

i have done the same thing by its not working ,says invalid syntax

Simple and efficient than mine !! Thanks

will you please explain the program implementation program?

Hey, I have heard that int(raw_input()) is faster.

raw_input() was renamed to input() in python 3

Can you tell me how python carries out list comprehension internally? How come this generates lists in lexicographic increasing order (i.e z increases first, then y, then x)?

It is in lexographical order due to the nature of the loops. Spaced out it looks somewhat like

We start at [0,0,0]. Then c will increment to get to [0,0,1] When c hits [0,0,z], we get [0,1,0] as the next in the loop. This pattern continues and gives us the lexographical ordering required of the output.

it's very helpfull to understanding that what is actualy asking

Thank you. This is the a verbose response, but clearly the most elegant to understand what's going on.

yup! i understand.

Thanks for your solution, now we understand how it's word.

Here is my soltion with your sussgestion:

empty_list = [] for a in range ( x+ 1): for b in range ( y + 1): for c in range ( z + 1): #logic code if (a + b + c != n) : empty_list.append([a,b,c])

print(empty_list)

But my program is not working properly.

Thank you, I really wondered on how it works.

really much helpfull in understanding

This solution is good! Utilising list comprehension to a good extreme.

Not reqired to put input() for each variable .just call the input for _ in range(no of variables); see here

a , b = int (input()) for _ in range ( 2 ):

Thanks for sharing your elegant code. I'm a newbie to Python, its great to see such beautiful code :)

to whome you added the comment :)

great code

I don't like these "direct print" solutions. In my opinion the problem stated to

createa list thenprint it, not just print something in a list. In other words in your solutions the list itself doesn't exist, can not be used later, it just collected when printed out. What do you think?OK, I found the answer! I can put list = instead of printoing out, then print the list. It wasn't clear for me three for loops output is collected in a list, so no need for any append tricks!

Nice answer. As an alternative, to avoid multiple for loops you can use product:

from itertools import product

how is this working?

it is not solution, as question was to form list comperhention. You just put out data to screen in right format, no list comperhetion created

Could someone explain to me what, exactly, this code does (like, statement-by-statement)(other than the input part I understand that I mostly mean the list comprehension)? I sort of have a basic understanding of how it works, but having some trouble wrapping my head around list comprehensions and how exactly the work. Mostly the "for a in range(0,x+1)" parts. What exactly does that do? Why is it (0,x+1)? Thanks in advance.

Just search for something like 'python range' and you will get pages like

this.just for curiosity, why you are using range(0,x+1) in the print function?

why do you need two square brackets? Can someone explain this to me?

hello.. I tried to do as u said. But it is showing syntax error.

from itertools import product

print([list(each) for each in product(range(x+1),range(y+1),range(z+1)) if sum(each)!=n])

I'm new to python, but how come nobody seems to be using ":" after their for loops. You don't even use one for the if loops at the end.

When you were using for statement inside the print statement you didn't use a colon infront of the for statement???? why was that

exactly what I had, expect I left the 0 implicit. Is it good practice to make the range explicit?

For this program it is fine. But in real time data range may not be static.

no need for 0 in range

how is list comprehension workin here?

Good one!!!!

I have no idea what is the syntax error that the compiler is showing repeatedly on (a+b+c)!=n

Thank you very much bro..... i am new to python... initially i din't understand your code but i realized after converting it into normal form. x, y, z, n = int(input()), int(input()), int(input()), int(input()) for i in range(0,x+1): for j in range(0, y + 1): for k in range(0, y + 1): if i + j + k !=n: print([i,j,k])

Thanks for your solution!

why x+1?

wrong answer

This is not usable with python 3 , shows error..!!

i got it in google chrome but not in edge i dont know the issue

thank you for commenting

this code does print in lexiocgraphy format but not the expected out

if possible, can you please explain me how it will how

why range x+1

can be :

print([[a, b, c] for a in range(x + 1) for b in range(y + 1) for c in range( z + 1) if a+b+c != n ])

print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if a + b + c != n ])

sir can you explain this line

@rnamaye this line print the value of a,b,c from 0 to x+1 , y+1 ,z+1 respectively if the sum (a+b+c) is not equal to the n

okay sir and one more doubt regarding to that only (a+b+c) !=n

Thanks Bro

can u explain me the working of this code ??

can anyone explain how this code works from print.........

Y aare we supposed to run the loop from 0,x+1

why range(0,x+1) ? why x+1 and not x ?

why range 0 , x+1

thank you

@gkeswani92 - can you please confirm what will be the order of complexity for this code ? is this still considered a nested loop or is this considered linear ?

x, y, z, n = int(input()), int(input()), int(input()), int(input()) print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if x + y+z != n ]) i have written the above code,but it is counting the a+b+c= n entries also..can someone explain why?

Hi Niket, The problem lies in your if condition, you're running loop with a,b,c but your comparing with it with input limit variables. Try the below snipped of code I'm sharing and this should work :

x, y, z, n = int(input()), int(input()), int(input()), int(input())

print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if (a + b + c) != n ])

Still what about its complexity?? Its still O(n^3)

is there a way to reduce the number of 'for in range' ?