2

How would I convert the following string to an array with python (this string could have an indefinite number of items)?

'["Foo","Bar","Baz","Woo"]'

This is definitely a string representation as well. type() gave:

<class 'str'>

Update:

Got it.

interestedin = request.POST.get('interestedIn')[1:-1].split(',')

interested = []

for element in interestedin:
    interested.append(element[1:-1])

Where request.POST.get('interestedIn') gave the '["Foo","Bar","Baz","Woo"]' string list "thing".

Aryan Beezadhur
  • 3,651
  • 3
  • 16
  • 39
GCien
  • 1,991
  • 6
  • 27
  • 55
  • 2
    That looks like a list, not a string... – sacuL Apr 25 '18 at 13:29
  • 7
    Possible duplicate of [How to convert a string to a list in Python?](https://stackoverflow.com/questions/5387208/how-to-convert-a-string-to-a-list-in-python) – code11 Apr 25 '18 at 13:30
  • are you talking about the element inside of this list ? – Ankush Rathi Apr 25 '18 at 13:30
  • That looks like it's a duplicate question – I am sure I've seen at least several variants. Anyway, what did you try & why did it not work? – Jongware Apr 25 '18 at 13:31
  • 3
    `ast.literal_eval()` or `json.loads()` ... – zwer Apr 25 '18 at 13:31
  • Im guessing he wants "".join(str_list), and has a very weird way of writing a question. – Rohi Apr 25 '18 at 13:32
  • Making a variable by Copy/paste of your 'following string' definitely does not return a class of str when you run type(). At least on my machine. – dfundako Apr 25 '18 at 13:35
  • Wow - -10 XD yes, it's definitely a string. I promise. type(my_variable) gives ``. I missed off the bracing '. – GCien Apr 25 '18 at 13:38
  • *It's come from passing an array via AJAX post request. – GCien Apr 25 '18 at 13:40
  • Got it - just updated my question. Apologies for asking such a weird question! :) – GCien Apr 25 '18 at 14:04
  • Does this answer your question? [Convert string representation of list to list](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – Georgy Dec 13 '19 at 14:10

4 Answers4

19

You can do this

import ast

list = '["Foo","Bar","Baz","Woo"]'
list = ast.literal_eval(list)
print list
Lex Bryan
  • 655
  • 8
  • 16
5

Dealing with string '["Foo","Bar","Baz","Woo"]'

str = '["Foo","Bar","Baz","Woo"]'
str1 = str.replace(']','').replace('[','')
l = str1.replace('"','').split(",")
print l # ['Foo', 'Bar', 'Baz', 'Woo'] A list

If you mean using python array module, then you could do like this:

import array as ar
x=ar.array('c')  #char array
for i in ['Foo', 'Bar', 'Baz', 'Woo']: x.extend(ar.array('c',i))
print x  #array('c', 'FooBarBazWoo')

It will be much simpler if you consider using numpy though:

import numpy as np
y=np.array(['Foo', 'Bar', 'Baz', 'Woo'])
print y #  ['Foo' 'Bar' 'Baz' 'Woo']
txicos
  • 241
  • 3
  • 5
  • First option ruins your data if any of the elements contain a [ or ] as part of its own text. Second and third options do not start from a string and have nothing to do to string to array or list conversion. – drakorg Aug 05 '20 at 02:43
5

No-package solution

You can use eval function

list = eval('["Foo","Bar","Baz","Woo"]')
print (list)

# ['Foo', 'Bar', 'Baz', 'Woo']

Alternative Solution

Based on @baptx comment, an alternative way (probably a better one) is to use ast package:

from ast import literal_eval 
list = literal_eval('["Foo","Bar","Baz","Woo"]')
print (list)

# ['Foo', 'Bar', 'Baz', 'Woo']
Hadij
  • 2,380
  • 5
  • 22
  • 38
  • It looks like this can be dangerous if the data is untrusted since it can execute code, I read it is recommended to use `ast.literal_eval` instead: https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval – baptx Jul 02 '21 at 14:17
0

you can also do this

import json

json.loads('["Foo","Bar","Baz","Woo"]')
flaxel
  • 3,429
  • 4
  • 13
  • 26