-1

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?

sebtheiler
  • 2,159
  • 3
  • 16
  • 29

3 Answers3

0

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,407
  • 2
  • 7
  • 21
0

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

eval("".join(list(str([0, 0, 0]))))
taras
  • 6,566
  • 10
  • 39
  • 50
0

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,748
  • 10
  • 49
  • 105