-2

If I type:

>>> list(str([0, 0, 0]))

I get: ['[', '0', ',', ' ', '0', ',', ' ', '0', ']']

How would I make it so that I get [0, 0, 0] instead?

user-124812948
  • 1,354
  • 2
  • 11
  • 20

3 Answers3

-1

If what you want to do is cast every item into a string. Do this:

>>> orig_list = [0, 0, 0]
>>> new_list = [str(item) for item in orig_list]

>>> new_list
['0', '0', '0']
N Chauhan
  • 3,327
  • 2
  • 5
  • 20
-1

It is not a good idea to use in a production code but you can use

eval("".join(list(str([0, 0, 0]))))
taras
  • 5,922
  • 10
  • 36
  • 48
-1

You can use list comprehension like this:

my_str = str([0, 0, 0])
my_list = [int(s) for s in my_str.replace('[', '').replace(']', '').split(',')]

Now you have:

>>> my_str
'[0, 0, 0]'
>>> my_list
[0, 0, 0]
nnyby
  • 4,452
  • 9
  • 48
  • 102