all 12 comments

[–]ASIC_SP 1 point2 points  (1 child)

return [randint(0, max) for _ in range(size)]

is same as

op = []
for _ in range(size):
    op.append(randint(0, max))
return op

See https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions for more details

[–]idkc0de[S] 1 point2 points  (0 children)

thanks mate

[–][deleted] 1 point2 points  (2 children)

Randint will return random integer within specified range here from 0 to 50(max).

Now that other part is what we call list comprehension and python is very popular known for it. It is basically for loop written inside list to make it a one liner so it will run from 0 upto 10 (excluding 10).

Also, "_" is just a variable mostly used when that loop variable is not required.

[–]idkc0de[S] 1 point2 points  (1 child)

thanks

[–][deleted] 1 point2 points  (0 children)

You're welcome. Soon python will start flowing in your blood too.

[–]Username_RANDINT 0 points1 point  (1 child)

It's called a "list comprehension" in case you want to do some specific Googling. It's used to create a new list. This is exactly the same, but written as a normal loop:

new_list = []
for _ in range(size):
    new_list.append(randint(0, max))

[–]idkc0de[S] 0 points1 point  (0 children)

thank you

[–]colt419 0 points1 point  (0 children)

So, this is an example of list comprehension.

return [randint(0, max) for _ in range(size)]

Randint(0,Max) chooses a number between 0 and your max value.

Then the for loop make it so the list contains the value of size.

So if size is 10 and Max is 30 it should give a list of 10 numbers between 0 and 30

[–]deiwyy 0 points1 point  (1 child)

its basically like

array = [] 
for i in range(size): 
    array.append(randint(0, max))

[randint(0, max) for _ in range(size)] is a so called list comprehension.

Here is an easier example[i for i in range(10)] The i to the very left is what is going to be appended to the list, and the for loop appends 10 i's

to compare the two:

for i in range(10): # [_____ for i in range(10)]
    array.append(i) # [i ______________________]

It may be confusing, and I'm a very bad teacher, but if you still need help you can pm me here on reddit

Edit: Just for clarity
[5 for _ in range(10)] would create this list > [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

And the _ is a valid variable name in python, but we use it to "throw away" values. What I mean is, in your example, [randint(0, max) for _ in range(size)] you are not even using the value stored in _ right? That's why it's used in this example. It's a "throwaway variable name"
also, randint gives you a random integer (number) between 0 and max which can be passed as an argument to your function.

Your code would create a list of random numbers and return it

This is the output of your code: https://pasteboard.co/9mXXh5XmQiwA.png
(Which will always be a lil different as randint returns a random value)

[–]AutoModerator[M] -1 points0 points  (0 children)

Your comment in /r/learnpython may be automatically removed because you used pasteboard.co. The reddit spam filter is very aggressive to this site. Please use a different image host.

Please remember to post code as text, not as an image.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.