# [Tutor] selecting data from a list

Albert-Jan Roskam fomcl at yahoo.com
Sun Jan 18 20:30:01 CET 2015

```
----- Original Message -----

> From: Peter Otten <__peter__ at web.de>
> To: tutor at python.org
> Cc:
> Sent: Sunday, January 18, 2015 4:38 AM
> Subject: Re: [Tutor] selecting data from a list
>
> Colin Ross wrote:
>
>>  Hi all,
>>
>>  I am attempting to isolate a certain subset of data from the list
> "a" and
>>  then turn it into any array. To do so, I have written the following code:
>>
>>  import numpy as np
>>
>>  a = [0,1,2,3,4,5,6,7,8,9,10]
>>  b = [10,20,30,40,50,60,70,80,90,100,110]
>>
>>  for a in range(len(a)):
>>  if a > 5:
>>  print a
>>
>>  a_1 = np.array(a)
>>
>>  print a_1
>>
>>  The output is as follows:
>>
>>  6
>>  7
>>  8
>>  9
>>  10
>>  10
>>
>>
>>  As you can see, when I attempt to turn the list of numbers 6 through 10
>>  into an array I am left with it only printing out 10...
>>
>>  The desired result is: [6,7,8,9,10}
>>
>>  Any guidance would be greatly appreciated.
>
> I have a hunch that you may be looking for slicing:
>
>>>>  a = [0,1,2,3,4,5,6,7,8,9,10]
>>>>  b = [10,20,30,40,50,60,70,80,90,100,110]
>>>>  a[6:]
> [6, 7, 8, 9, 10]
>>>>  b[3:]
> [40, 50, 60, 70, 80, 90, 100, 110]
>
> If I'm right you should really work through a tutorial.

If Peter is right about his hunch, then ignore the following. If not, then you could use a Boolean array to do the selection:

>>> import numpy as np
>>> arr = np.arange(11)
>>> arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

>>> arr > 5
array([False, False, False, False, False, False, True, True, True,
True, True], dtype=bool)

>>> arr[arr > 5]
array([ 6,  7,  8,  9, 10])

Regards,
Albert-Jan
```