20

I get NameError: name 'array' is not defined in python error when I want to create array, for example:

a = array([1,8,3])

What am I doing wrong? How to use arrays?

3
  • 3
    Do you actually need an array, or do you just want a list? Commented Aug 17, 2011 at 20:23
  • numpy also has an array function. Is that what you mean? If so, you may need to prefix with the namespace. Commented Aug 26, 2016 at 19:34
  • If you want to use numpy to create an array, you need to do this: a = np.array([1, 8, 3]). You're missing the "np." in front of the "array" part in your code -- if this is what you're going for that is (i.e., using numpy to create arrays). Commented Jan 24, 2022 at 19:54

9 Answers 9

62

You need to import the array method from the module.

from array import array

http://docs.python.org/library/array.html

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

3 Comments

then I get this error TypeError: array() argument 1 must be char, not list
@Templar - array in Python isn't exactly like an array in languages like C or Java, they are constrained and you must specify a type that the array will hold. You can use lists like you would arrays in other languages, but otherwise, you have to specify that your array holds integers. Check out the first part of the doc: docs.python.org/library/array.html
primes = array('i', [2, 3, 5, 7, 11, 13])
25

For basic Python, you should just use a list (as others have already noted).

If you are trying to use NumPy and you want a NumPy array:

import numpy as np

a = np.array([1,8,3])

If you don't know what NumPy is, you probably just want the list.

1 Comment

I will just add the comment that the confusion likely comes from the fact that a numpy array outputs as array([1, 2, 3]), however you have to input them as np.array([1, 2, 3]). (After importing import numpy as np.)
2

If you need a container to hold a bunch of things, then lists might be your best bet:

a = [1,8,3]

Type

dir([])

from a Python interpreter to see the methods that lists support, such as append, pop, reverse, and sort. Lists also support list comprehensions and Python's iterable interface:

for x in a:
    print x

y = [x ** 2 for x in a]

6 Comments

can i insert things to list like for i in range(10): a[i] = b?
@Templar a = [b for i in range(10)] That will create a new list [b,b,b,b,b,b,b,b,b,b]
so it means firstly I ALWAYS must fill list with some random stuff if I want to use it like that?
@Templar no, you can use the append method to add to an existing list: a = []; a.append(1); a.append(2); a now has the value [1,2]. You can also access individual elements using index notation: print a[1], a[1] = 3, or slice notation print a[:2] -- prints the first two elements of a.
This did not answer the original question.
|
2

You probably don't want an array. Try using a list:

a = [1,8,3]

Python lists perform like dynamic arrays in many other languages.

1 Comment

Highest voted answer actually answers the question. Stay...defensive and unthinking.
1

You need to import the array.

from numpy import array

Comments

1

In python Import problem occurs when you accidentally name your working file the same as the module name. This way the python opens the same file you created using the same as module name which causes a circular loop and eventually throws an error.

This question is asked 10 yrs ago , but it may be helpful for late python learners

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

If you're trying to use NumPy, use this:

import numpy as np
a = np.array([1, 2, 3])

If not then a list is way more easier:

a = [1, 2, 3]

Comments

0

**from array import *** myarray=array('i',[10,39,48,38])

2 Comments

Hi. Your code isn't valid and could you explain your suggestion to make it an answer? Thx
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
0

Maybe you haven´t executed the cell. It worked for me

enter image description here

1 Comment

There was no mentioning of numpy being used by the OP. Please stick to what tags are set for the question. If it is asking about python and arrays don't show an example using a non-standard library without explaination

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.