0

I have been trying a lot to make arrays and this is my latest one:

from array import *
Romanic=array['str',('Italian','French','Spanish','Portugese','Romanian')]
print('Romanic languages are ', Romanic, 'Want to insert more?')

But an error pops up:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: 'type' object is not subscriptable
> 

More or less the same happens in most codes related to python and I don't understand what is wrong with my code. Maybe arrays aren't made for strings? But that's not clear anywhere. And after searching about the error, it has nothing to do with my code as multiple variable names I have used have the same problem.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
ToxicDMG
  • 3
  • 2
  • 1
    array.array (array in your case) is a function. You should invoke it with () not []. Also, 'str' doesn't seem to be supported: https://docs.python.org/3/library/array.html -- are you just trying to use a list? `Romanic = ['Italian', 'French']` or `Romanic = list(['Italian', 'French'])` – nitobuendia Jan 30 '21 at 08:05

3 Answers3

1

The array module only allows to create array of basic values (characters, integers, floating point numbers).

You cannot create an array of strings with this module.

More information in the documentation here

I believe using a list would more adapted to your needs:

Code

Romanic=['Italian','French','Spanish','Portugese','Romanian']
print('Romanic languages are ', ', '.join(Romanic), '. Want to insert more?', sep='')

Output

Romanic languages are Italian, French, Spanish, Portugese, Romanian. Want to insert more?
Marc Dillar
  • 471
  • 2
  • 9
0

array defines an object type to compactly represent an array of basic values: characters, integers, floating-point numbers.

In your case, you are trying to define an array with strings and this is not possible.

Moreover, as pointed out by @nitobuendia in the comments of the question, array is a function so, you have to use round brackets () not the squared ones that instead are index operators.

Depending on your needs, you can use a tuple (immutable object), list (mutable) (see here difference between tuples and lists) or a numpy array:

import numpy as np

Romanic = ('Italian','French','Spanish','Portugese','Romanian')  # tuple
Romanic = ['Italian','French','Spanish','Portugese','Romanian']  # list
Romanic = np.array(['Italian','French','Spanish','Portugese','Romanian'])  # numpy array
PieCot
  • 3,564
  • 1
  • 12
  • 20
0

There are a number of problems with your code.

First, there is an array module and part of it, there is an array.array class. When you do

from array import *
Romanic=array['str',(...)]

you are treating array.array as an actual array object, instead of a class. Using [] in Python means accessing an element of something, not for initialization. You should be using ():

>>> from array import array
>>>
>>> array['i',(1,2,3)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
>>>
>>> array('i',(1,2,3))
array('i', [1, 2, 3])

Now, even if you changed your code to array('str',...), it still wouldn't work, because the 1st argument to the class initialization should be a valid typecode.

https://docs.python.org/3/library/array.html#array.array

class array.array(typecode[, initializer])

A new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a list, a bytes-like object, or iterable over elements of the appropriate type.

The array module docs has a table for all the typecodes, and you can also get the valid typecodes by:

>>> from array import typecodes
>>> print(typecodes)
bBuhHiIlLqQfd

and 'str' is not in that of valid typecodes. Maybe you are coming from other programming languages that use arrays, but what you seem to need is just a regular list, which in Python is how we represent a mutable "list of things":

>>> Romanic = ['Italian','French','Spanish','Portugese','Romanian']
>>> print('Romanic languages are ', Romanic, 'Want to insert more?')
Romanic languages are  ['Italian', 'French', 'Spanish', 'Portugese', 'Romanian'] Want to insert more?

Lastly, don't use from module import * syntax. See Why is “import *” bad?. It can become confusing, like in this case with an array module and an array class. Import the actual things you need from the module, or use module.something syntax.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135