1
from array import *
val=array('u',["thili","gfgfdg"])
print(val)

When I compiled above python code, the compiler showed an error.

enter image description here

What is the problem in my code.can not store strings in python array?

4
  • Please read through stackoverflow.com/help/how-to-ask to help you forming a question which will get better answers. Commented Nov 15, 2019 at 17:37
  • 2
    why would you want to do that? check the docs - you could use array to hold characters (one string basically) but it is not intended to hold strings. Commented Nov 15, 2019 at 18:18
  • 2
    No, but why would you need this? Generally, you would just use a list. Commented Nov 15, 2019 at 18:21
  • val=array('u', 'thili gfgfdg').tounicode() Let me know if it works for you. Commented Nov 15, 2019 at 18:32

2 Answers 2

0

Firstly, the Python interpreter is written in C language language and that array library is includes array of C language (In fact, it is not array of Python, it is array of C). A string is array of chars in C (char is a number which act like one letter). You are passing two unicode strings as argument to the array function but one of these unicode strings is already an array for C. So you cant pass two unicode string to array function. Look at that:

from array import array
my_array = array("u","thili") # no error
print(my_array) # array('u', 'thili')

other_array = array("u",["thili","gfgfdg"])
Traceback (most recent call last):
  File "<pyshell#5>", line 3, in <module>
    my_array = array("u",["thili",""])
TypeError: array item must be unicode character

As you can see array of unicode string is not much different than normal unicode string. Because it contains only one string. You should use list or tuple class instead. And the list class in Python is array of Python.

my_list = ["thili","gfgfdg"] # same as: my_list = list("thili","gfgfdg")
my_tuple = ("thili","gfgfdg") # same as: my_tuple = tuple("thili","gfgfdg")

Dont forget that tuples are unmutable but lists are mutable. If you want to change value of any index, then use list. Tuples are good when you want to optimize your memory (RAM) usage. Finally tuples are more faster than lists in terms of creation.

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

4 Comments

" And list are more faster than tuples while indexing" No, they are not.
Yes, they are (At least in my computer and python3.7). You can try it by using timeit module. Lists are a bit faster (But not slower). Try that: """ from timeit import timeit \na=list(range(100)) ; b=(range(100)) \ndef f(c): \n\tfor i in c: pass \nprint("List:",timeit(lambda: f(a))) ; print("Tuple:",timeit(lambda: f(b))) """
That compares iterating over a list versus iterating over a range object, not indexing, i.e. my_sequence[0] between lists and tuples
Yes , i made an mistake. Thank you. But i am trying it with b=tuple(range(100)) and list is still faster. ???
-1

You can not find anything faster or not by using timeit module in python or any programming language because time taken directly depends upon your machine configuration so for checking that thing we use time complexity in programming...

2 Comments

I dont think the OP's question was about how fast tthe code will run. Please review the question again. This does not answer the question.
This does not answer op's question.

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.