1

I am using Repl.it.

import array as arr

my_array = arr.array("u", [u"3", u"6", u"9", u"12"])
print(my_array)
print(type(my_array))
print(type(my_array[0]))

The above source code produces the following error:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    my_array = arr.array("u", [u"3", u"6", u"9", u"12"])
TypeError: array item must be unicode character

Why the source code isn't working?

3
  • Which version of python you are using? Commented Oct 4, 2020 at 17:19
  • @AbdulNiyasPM, I am using Repl.it. Commented Oct 4, 2020 at 17:20
  • I mean your sys.version? Commented Oct 4, 2020 at 17:24

1 Answer 1

2

Array type "u" corresponds to a single Unicode character. The initializer contains a two-character item u"12". Perhaps you want something like:

arr.array("u", [u"3", u"6", u"9", u"1", u"2"])
Sign up to request clarification or add additional context in comments.

3 Comments

It can also be initialized more simply as my_array = arr.array('u','36912').
@MarkTolonen Yes. I assumed OP has to use a list since it was in the question
It was just a general comment. The OP may not know it is easier to type.

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.