5

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'>

I 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".

11
  • 2
    That looks like a list, not a string... Commented Apr 25, 2018 at 13:29
  • 7
    Possible duplicate of How to convert a string to a list in Python? Commented Apr 25, 2018 at 13:30
  • are you talking about the element inside of this list ? Commented Apr 25, 2018 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? Commented Apr 25, 2018 at 13:31
  • 3
    ast.literal_eval() or json.loads() ... Commented Apr 25, 2018 at 13:31

4 Answers 4

23

You can do this

import ast

list = '["Foo","Bar","Baz","Woo"]'
list = ast.literal_eval(list)
print list
Sign up to request clarification or add additional context in comments.

2 Comments

@GCien This should be marked as the correct answer.
That seems the way to go to get the Life boat badge: Add an answer without any explanation whatsoever.
7

No-package solution

You can use the eval function:

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

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

Alternative solution

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

from ast import literal_eval

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

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

2 Comments

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: stackoverflow.com/questions/15197673/…
Though this answer is about Python (about which, I admit, know nothing), not about PHP, but even so... I still believe that this statement is true even for Python: "Rasmus Lerdorf, the creator of PHP, once wrote that »if eval() is the answer, you're almost certainly asking the wrong question«"!
4

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 the Python array module, then you could do like this:

import array as ar

x = ar.array('c')  # Character 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']

1 Comment

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.
2

You can also do this:

import json

json.loads('["Foo", "Bar", "Baz", "Woo"]')

Comments

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.