Python Forum

Full Version: Please help in understanding this piece of code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How this code
np.array([range(i, i + 3) for i in [2, 4, 6]])
results in
Output:
array([[2, 3, 4], [4, 5, 6], [6, 7, 8]])
Thanks
you are building an array out of a list which is created using a list comprehension.
this specific list comprehension picks each number in [2, 4, 6], one at a time and using the range function. the range(lower_bound, upper_bound) function itself returns a list containing all numbers from lower_bound to upper_bound - 1. So your list will contain 3 sublists. for the first one range will be called with 2, since it is the first one in [2, 4, 6]. So range(2, 5) == [2, 3, 4]. The second list is made from i = 4, so that range(4, 7) will return [4, 5, 6]. And finally using i = 6 will result in range(6, 9) == [6, 7, 8]. so that your complete list will look like this: [[2, 3, 4], [4, 5, 6], [6, 7, 8]]. And out of that you are creating an array :)
I was thinking about the OP's question... Wouldn't a "normal" python session produce a list of 3 range objects when [range(i, i + 3) for i in [2, 4, 6]] is called?
range always produces an output of type list. The xrange function on the other hand does not ;) But nevertheless if a list containing 3 lists or 3 range objects is given to np.array() they will be converted to lists and a normal array is made :)
(May-10-2018, 10:46 AM)ThiefOfTime Wrote: [ -> ]you are building an array out of a list which is created using a list comprehension.
this specific list comprehension picks each number in [2, 4, 6], one at a time and using the range function. the range(lower_bound, upper_bound) function itself returns a list containing all numbers from lower_bound to upper_bound - 1. So your list will contain 3 sublists. for the first one range will be called with 2, since it is the first one in [2, 4, 6]. So range(2, 5) == [2, 3, 4]. The second list is made from i = 4, so that range(4, 7) will return [4, 5, 6]. And finally using i = 6 will result in range(6, 9) == [6, 7, 8]. so that your complete list will look like this: [[2, 3, 4], [4, 5, 6], [6, 7, 8]]. And out of that you are creating an array :)

Thanks you very much, You explained the concept in an excellent way.
(May-10-2018, 11:03 AM)ThiefOfTime Wrote: [ -> ]range always produces an output of type list. The xrange function on the other hand does not ;) But nevertheless if a list containing 3 lists or 3 range objects is given to np.array() they will be converted to lists and a normal array is made :)

So it is indeed a property of np.array(), that converts range objects to lists when passed to it?
(May-10-2018, 10:51 AM)j.crater Wrote: [ -> ]I was thinking about the OP's question... Wouldn't a "normal" python session produce a list of 3 range objects when [range(i, i + 3) for i in [2, 4, 6]] is called?

Yes, it would - but I presume numpy does not have dtype of range - so it implicitly converts range to list of values. "Batteries included"
(May-10-2018, 11:23 AM)volcano63 Wrote: [ -> ]
(May-10-2018, 10:51 AM)j.crater Wrote: [ -> ]I was thinking about the OP's question... Wouldn't a "normal" python session produce a list of 3 range objects when [range(i, i + 3) for i in [2, 4, 6]] is called?

Yes, it would - but I presume numpy does not have dtype of range - so it implicitly converts range to list of values. "Batteries included"

Yeah, that's what I imagined must be going on. Thanks :)
(May-10-2018, 11:03 AM)ThiefOfTime Wrote: [ -> ]range always produces an output of type list. The xrange function on the other hand does not ;) But nevertheless if a list containing 3 lists or 3 range objects is given to np.array() they will be converted to lists and a normal array is made :)

In Python 3, range produces object of type range, and xrange is deprecated.

In general, all Python2 functions/methods that would produce list - dict.items, zip, range, map, filter (list itself excluded) - produce generator-like objects in Python3
Thanks everyone for sparing time and explaining the concept