1

I am trying to extract the values from a numpy array and place the individual entries into a list.

For example, if I have the following list:

import numpy as np
x = 1
problem_variable = np.array(['a', 'b'], dtype='<U9')
z = 2
mylist = [x, problem_variable , z]
# [1, array(['a', 'b'], dtype='<U9'), 2]

How do I get the result

[1, 'a', 'b', 2]

I do not know the length of problem_variable before hand so cannot hard code problem_variable[0], problem_variable[1] etc.

The following does what I want but I am sure that I am missing the appropriate way to break the array apart. Thanks.

result = []
result.append(x)
for i in problem_variable: result.append(i)
result.append(z)
yatu
  • 86,083
  • 12
  • 84
  • 139
user2957945
  • 2,353
  • 2
  • 21
  • 40

3 Answers3

4

you can unpack your array:

mylist = [x, *problem_variable , z]
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Thank you! Can you point me to some docs for `*` : i see this: https://docs.python.org/3.7/tutorial/controlflow.html#unpacking-argument-lists . edit: https://stackoverflow.com/questions/3480184/unpack-a-list-in-python gives example and refs – user2957945 Mar 31 '20 at 15:10
  • you may have a lool over: https://www.python.org/dev/peps/pep-0448/ – kederrac Mar 31 '20 at 15:16
  • 1
    Thank you kederrac – user2957945 Mar 31 '20 at 15:18
1

Since you're using numpy: you can use np.r_ to concatenate the input objects along first axis:

np.r_[x, problem_variable, z]
# array(['1', 'a', 'b', '2'], dtype='<U9')

Comparing performance on larger lists:

problem_var = np.concatenate([problem_variable]*10000, axis=0)

%timeit np.r_[x, problem_var, z]
# 143 µs ± 19.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit np.hstack((x, problem_var , z))
# 553 µs ± 55.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit [x, *problem_var.tolist() , z]
# 502 µs ± 42.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit [x, *problem_var , z]
# 6.46 ms ± 215 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
yatu
  • 86,083
  • 12
  • 84
  • 139
  • Thank you! thats what i was looking for. Do you know what would be a good search term for me to use for this please as I was finding anything appropriate. – user2957945 Mar 31 '20 at 15:06
  • 1
    I supose a good place to start is always the docs. Here reading on the different slicing methods should help, see https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html – yatu Mar 31 '20 at 15:09
  • Thanks Yatu, this actually fits my use case better as I am then coercing the list to an array (great SO mind reading skills there!) but I'll mark kederrac's as it gives the results format in the question. – user2957945 Mar 31 '20 at 15:14
  • Note that arrays have a `tolist()` method, which you can use to get a list back from the `array` @user2957945 – yatu Mar 31 '20 at 15:15
  • Added the timings also @user2957945 – yatu Mar 31 '20 at 15:16
  • That's really great. Appreciate it Yatu . So working with arrays is much faster – user2957945 Mar 31 '20 at 15:19
  • In the general case and if used properly yes @user2957945 , much faster – yatu Mar 31 '20 at 15:23
  • `[x, *problem_var.tolist() , z]` improves the time of the unpacking approach. `arr.tolist()` is much faster than the iterative `list(arr)`. – hpaulj Mar 31 '20 at 15:37
  • Indeed it does! Didn't expect the difference to be so large because of using the list constructor @hpaulj – yatu Mar 31 '20 at 15:41
1

You can use np.hstack, which can stack arrays in sequence horizontally,

np.hstack((x, problem_variable , z))

# ['1' 'a' 'b' '2']
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53