## List Comprehensions

gkeswani92 + 15 comments `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 ])`

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

`x, y, z, n = (int(input()) for _ in range(4))`

gkeswani92 + 0 comments Thanks for the input. Will use this from now! :D

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

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

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

- BH
codeharrier + 0 comments 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.

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

AbhishekVermaIIT + 0 comments 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.

- MI
alfredocambera + 1 comment Doesn't work on python3.5

- AJ
akik22 + 0 comments [deleted]

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

NervousBlakedown + 0 comments Disregard, I figured it out.

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

- NK
Code_Lord + 0 comments 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.

- RT
rahul_rowthi + 0 comments How is this syntax even valid. Just wondering.

aa1992HackerRank Admin + 5 comments nice solution. But instead of range[0,x] you could do

`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 ])`

ericali + 0 comments clever!!!

- RM
rajasekhariiith + 0 comments [deleted] - VP
SuperGogeta + 3 comments Trade three +1's for a +1 and a -1 at the right place :P

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

zhangqunshi + 0 comments although this method is more simple, but it not easy to understand.

- YY
yidingyang1027 + 0 comments great!

- US
usaliev + 0 comments I liked this method compared to other's.

james_niroo + 0 comments really like this solution. simple and easy to read

- MT
abedsarkil12 + 0 comments [deleted]

rachitajitsaria1 + 1 comment Hey, I have heard that int(raw_input()) is faster.

maximshen + 0 comments raw_input() was renamed to input() in python 3

- MB
boler + 0 comments [deleted] - RD
rahatchd + 2 comments 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)?

Ryukerg + 4 comments 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.

BHEEMKUMAR + 0 comments it's very helpfull to understanding that what is actualy asking

masudraj6 + 0 comments Thanks for your solution, now we understand how it's word.

nick3499 + 0 comments [deleted]

nick3499 + 0 comments [deleted]

SanjayDeepu + 0 comments This solution is good! Utilising list comprehension to a good extreme.

trinadhkoya + 0 comments 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 ):

- BR
bennetryan + 0 comments Thanks for sharing your elegant code. I'm a newbie to Python, its great to see such beautiful code :)

jonobjornstad + 0 comments great code

- LA
andras_lengyel + 1 comment 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?- LA
andras_lengyel + 0 comments 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!

- IC
ignacio_ch + 0 comments Nice answer. As an alternative, to avoid multiple for loops you can use product:

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

- BR
raspletin + 0 comments 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

- SM
pH03nYx + 1 comment 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.

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

- LY
ldhyim568 + 0 comments why do you need two square brackets? Can someone explain this to me?

gabobaby + 0 comments The problem states that it wants the output list printed in 'increasing order'. It should be more clear what this means. I initially interpreted this to mean that [1,1,0] would come before [3,0,0] because 1+1+0=2 is less than 3+0+0=3. But this is not the order that the comprehension list method would generate the list. And would likely involve some method of sorting a 3D array that many new users would not know at this point, so I'm assuming this is not what is meant. This problem is not clarified with the chosen example values of x=1, y=1, z=1, and N=2.

The problem could be addressed by just using larger example values, allowing us to infer from the example output which interpretation of 'increasing order' is intended.

scintilla + 2 comments What does N represent? Why should X+Y+Z not equal N? And what do the subscript i's represent? I don't understand the question.

- SK
saikiran9194HackerRank Admin + 2 comments @scintilla read the question properly. 0<=Xi<=X same for Yi and Zi.N is specified in the question.

- JL
jhub1 + 0 comments I am sooooooooooooo confused with the question holy cow.

- JH
ID10TERROR + 1 comment You can't just say "Read the question properly". He said he doesn't understand the syntax of the question.

This is a type of mathematic short-hand that not all of us understand. I for one don't even know what this short-hand is called or I'd look it up myself.

shashank21jHackerRank Admin + 2 comments Try now, I have updated the statement :)

- JH
ID10TERROR + 1 comment Thank you for the follow-up shashank21j, your revision to the statement has been helpful, but I still have some questions about the way the question is worded and why.

1.Why must i+j+k not be equal to N and does this mean it can be greater than N?

2.Please define "lexicographic increasing order"

I for one agree that list comprehension in python is a fairly simple subject, the crux of this problem for me however, is bridging the communication gap/understanding the essance of the problem you're asking.

Many people who use your site are self taught in coding and do not have a formal discrete mathematics background.

shashank21jHackerRank Admin + 1 comment i + j + k != N is just an added condition in this problem so you can use 1 if statement inside your list compehension syntax. It can be greater than N as long as i, j and k are in their respective limits.

- Lexicographic order is sorted order where 1, 1, 1 comes before 1, 1, 2 and 2, 1, 2 comes before 2, 2, 1 etc. etc.

- JH
ID10TERROR + 0 comments Thanks again shashank21j! :)

- DN
dng304 + 0 comments Shouldn't the statement include that X,Y,Z are integers that are possible maximums? It is a fairly important point no? For example dimensions of 2,2,2 given the statement as it currently is, means there is only 1 possible cube with 8 coordinates.

Where as if we said that 2 is the max integer value for X,Z,Y , we can have more than 1 possible cuboid. For example a 1,1,2 cuboid.

- MK
mike_kinny + 0 comments I have also difficulty understanding the question.

Tsean + 1 comment Can anybody make me understand this question ? I went through few comments what this question all about but still I'm not clear Please help

eric2013Asked to answer + 4 comments You're given 4 numbers. The first 3 correspond to the maximum dimensions of a cube. y is the maximum height, x is the maximum width, and z is the maximum depth.

You're supposed to calulate every possible set of dimensions [x,y,z], under the condition that none exceed the input values, and that x+y+z does not add up to n.

- CR
steeeenos + 0 comments thanks that was very helpful!

- SS
shashanksaurav41 + 1 comment Thanks for the explanation eric. I've just one more question that in this lexicographic order the last dimension is [1,1,1], but our N is given to be 2. Thus the sum of x+y+z becomes 3 which is greater than N. How is this condition satisfied?? Pls explain.

eric2013 + 1 comment The only restriction relating to N is that i + j + k != N. i+j+k>N is perfectly valid.

- SS
shashanksaurav41 + 0 comments Thanks for your help. Was helpful and appreciate that

- SS
sawani_soman92 + 0 comments Helpful explanation! Thank you

- DN
dng304 + 0 comments I agree that what you say is the intention of the challenge, But my problem with this challenge is that it never states that X,Y,Z are maximums.

Sample input of 2,2,2 can only be 1 cube with 8 dimensions.

I feel like the statement should be change to express what you are saying, that X,Y,Z correspond to maximum integer dimensions.

fisher6 + 0 comments print([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if sum([i,j,k]) != n])

- J
jsleirer + 2 comments I feel like I am missing the point of this exercise with my solution. Can someone please comment on how to make my code more pythonesque and utilize list comprehensions better.

`X = int(raw_input()) Y = int(raw_input()) Z = int(raw_input()) N = int(raw_input()) Xi = [x for x in range(X+1)] Yi = [y for y in range(Y+1)] Zi = [z for z in range(Z+1)] results = [] for x in Xi: for y in Yi: for z in Zi: if x + y + z != N: results.append([x,y,z]) print results`

shashank21jHackerRank Admin + 0 comments this is fine.

Xi Yi and Zi are not much of use here. You can make your code smallerstcrestrada + 2 comments cuboid = []

results = [cuboid.append([x, y, z]) for x in range(X+1) for y in range(Y+1) for z in range(Z+1) if x + y + z != N]

print(cuboid)

Less lines, more Pythonic

PRASHANTB1984 + 1 comment That's really great :)

Shriswissfed + 1 comment print ([[a,b,c] for a in range(int(input())+1) for b in range(int(input())+1) for c in range(int(input())+1) if a+b+c!=int(input())])

why does this code not work? can you explain?

- VS
vinayshashank + 0 comments The inputs should be captured before the for loop is executed. In this case, a new input is expected for every iteration of the for loop. Try capturing the inputs first inside some variables and use those variables here. That will work.

gkeswani92 + 1 comment You don't even need to have the cuboid list to be honest :)

`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 ])`

stcrestrada + 0 comments ha!, I realized that a day later. Thanks for clearing it up though gkeswani92

adamxu59 + 0 comments listCoords = [[x1, y1, z1] for x1 in range(x+1) for y1 in range(y+1) for z1 in range(z+1) if x1+y1+z1!=n]

- S
shubhamgorawara + 0 comments My code:

import itertools x = int(raw_input()) y = int(raw_input()) z = int(raw_input()) n = int(raw_input()) print([ [i, j, k] for i, j, k in itertools.product(range(x+1), range(y+1), range(z+1)) if i+j+k != n ])

niketanmoon + 0 comments Prompts made it easy to solve. Explanation helped. Here's my code: 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])

fcueto + 0 comments Instead of using nested comprehensions, I used itertools to create the cartesian product:

import itertools l = [[i,j,k] for (i,j,k) in itertools.product(range(x+1), range(y+1),range(z+1)) if i+j+k!=n]

Sort 134 Discussions, By:

Please Login in order to post a comment