range function simply doesn't work like the way you have coded it. Basic syntax of range is range(starting value, ending value, steps) and that is what you are doing here. Also, 'for' statement doesn't take multiple variables like that for answering the question in the video. For applying your logic properly, you can use a library called 'itertools'. A proper solution will be as follows: import itertools res = 0 x=1 y=2 z=1 n=3 res=[[i,j,k] for i,j,k in itertools.product(range(x+1),range(y+1),range(z+1)) if i+j+k!=n] print(res) In the above code, I have used the product method that returns the cartesian products of the iterables defined inside it's bracket. You can visit the site I am mentioning for knowing more about this method. www.geeksforgeeks.org/python-itertools-product/ happy coding!
I was looking for an explanation for this problem and you did it perfectly. Really Thank you for the explanation.
Glad it helped!
thank you sir well explained
Glad it helped😊. Please do share with your friends too😊
you really explained it very well.
Nicely explained 👍
Thank you 🙂
Thanks sir very helpful video .I am a begginer this video helped me a lot to understand list comprehension.
Glad to hear that
Good info 👍
Glad it was helpful!
simple and nice explainiation
Thanks for liking
explained very well, awesome thank you, do more videos.
Thanks, will do!
ur explanation is awesome sir thank you
Glad you liked it. Do share with your friends too😊
Sure 👍
Thankyou you explained it very well
Glad you liked it 😊
Great 👌
Glad you liked it 😊.Do share with your friends too 🙂
This is very well explained, though the only thing I don’t get is why when you create the ranges you add 1 to the variable, e.g. range(x + 1)
if we would have written range(x) then range function will work for x-1 times. eg. for i in range(10) will return [0,1,2,3,4,5,6,7,8,9]
Good Job Brother, but why you don't use append in List Comprehensions?
Cool explanation Bro ..Thanks and keep sharing
Thanks and welcome
Hi. Why it needs to add 1?
y u didnt use append in list comprehension?
Why cant i get the output when i do the same code in my normal ide ? Explanations anyone?
Because HackerRank has shortcuts especially when it comes to the input
why this is wrong?
new_list = [ [ i , j , k ] for (i , j , k) in range (x+1, y+1, z+1) if (i + j + k ! = n)]
range function simply doesn't work like the way you have coded it.
Basic syntax of range is range(starting value, ending value, steps) and that is what you are doing here.
Also, 'for' statement doesn't take multiple variables like that for answering the question in the video.
For applying your logic properly, you can use a library called 'itertools'.
A proper solution will be as follows:
import itertools
res = 0
x=1
y=2
z=1
n=3
res=[[i,j,k] for i,j,k in itertools.product(range(x+1),range(y+1),range(z+1)) if i+j+k!=n]
print(res)
In the above code, I have used the product method that returns the cartesian products of the iterables defined inside it's bracket.
You can visit the site I am mentioning for knowing more about this method.
www.geeksforgeeks.org/python-itertools-product/
happy coding!
LIST COMPREHENSION
not multiple for loops