1

I'm new to Python programming. I'm reading Arrays in Python and when I want to create an array in Python, I need to import this module array.

For integers I write like this : ('i',[1,2,3]) For floating I write like this : ('d',[1.1,2.3])

Now I need to create an array like this for example ['A','B','C']. How could I create it by using the array module?

3
  • 2
    Are you sure you want an array? Lists are generally better for string elements unless you know all the elements are going to be, say, one character, or three, or some other fixed width. Commented Aug 1, 2019 at 3:37
  • @Linuxios I know about the list, dear Sir. But I wanted to know if it is possible by using this module. Commented Aug 1, 2019 at 3:41
  • 1
    Great -- I just wanted to make sure that the information on lists was here on the question for others who come across it later who might not know. It looks like Jack's answer below answers about array. :) Commented Aug 1, 2019 at 3:42

1 Answer 1

3

The docs state that it's used for:

Efficient arrays of numeric values

However I think the u (Unicode character) will suffice for your needs

("u", ["A", "B", "C"])

It would be generally better to make a standard Python list, though:

myList = ["A", "B", "C"]

Because this means you aren't bound by any restrictions on what data type your array could contain.

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

3 Comments

Thank you for your reply, but why it gave me the results like this? array('u', 'abvccd')
Because I believe it's for a series of Unicode characters - see the link to the documentation for an example.
OK, dear Sir. Thank you so much. :)

Your Answer

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