2

I want to convert python 2d list,

[[1,2,3],[4,5,6]]  

to

python3 array.array type,

[ [1 2 3]

[4 5 6] ]

The most important challenge is I wont import numpy. So I cant use np.asarray().

I have also used array.fromlist() and array.extend(). Both methods are creating an array of single dimension. But I want to convert multi dimension list to multi dimension array. Is there any way?

2
  • I want to convert to python array module fro python list Commented Sep 1, 2018 at 5:21
  • 1
    numpy.ndarray has attributes like shape and strides which allow it to represent multidemensional arrays. Python array does not have those. It also does not implement an object (or pointer) type, which would allow it to 'contain' lists or other objects. Commented Sep 1, 2018 at 6:21

1 Answer 1

4

What you're asking is not possible. From the array docs:

This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers ... The type is specified at object creation time by using a type code

The only supported types are as follows (notice the absence of list):

Type code            C Type        Python Type
    'b'         signed char                int
    'B'       unsigned char                int
    'u'          Py_UNICODE  Unicode character
    'h'        signed short                int
    'H'      unsigned short                int
    'i'          signed int                int
    'I'        unsigned int                int
    'l'         signed long                int
    'L'       unsigned long                int
    'q'    signed long long                int
    'Q'  unsigned long long                int
    'f'               float              float
    'd'              double              float 

It seems like you might be confusing the array module, with a native implementation of numpy arrays. This is not the case.

Sign up to request clarification or add additional context in comments.

4 Comments

yes I have read this. That's why I asked this question. It may be an indescent way to convert to python array. But I want to know a way to convert.
And as I clearly state in my answer. This is not possible. You cannot create a native array that contains list
What I mean is I dont want to create an array of list. I want to create an array of array from list of list.
Which is equally not possible. The closest you can get is a list of array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.