0

Well, I am using python. And I have case here.

from my api. The following key and value is coming.

games : ["['football','cricket']"]

Now i want to get that football and cricket from coming games value and store in python list.

expected output:

print(games) ==> ["football","circket"]
print(type(games))  ==> <class list>
davidism
  • 121,510
  • 29
  • 395
  • 339
Ahmed Yasin
  • 886
  • 1
  • 7
  • 31
  • 4
    Perhaps `ast.literal_eval` function would be useful here. Just pass the string into the function and a list of strings will be returned. [Docs linked here](https://docs.python.org/3/library/ast.html#ast.literal_eval) – S3DEV Sep 11 '22 at 21:35
  • the following StackOverflow question can help https://stackoverflow.com/questions/1894269/how-to-convert-string-representation-of-list-to-a-list – Mohamed Beltagy Sep 11 '22 at 21:38
  • Just use `json.loads()`. Highly unlikely that any api is sending any other than json string. – TheMaster Sep 11 '22 at 21:43
  • json.loads returns error on this value. I used that first – Ahmed Yasin Sep 11 '22 at 21:45

3 Answers3

4

Perhaps ast.literal_eval function would be useful here. Just pass the string into the function and a list of strings will be returned.

This will be more robust that trying to manipulate the string, as the function is designed to ingest (if you will) key Python data structures as strings; perform a bit of validation to ensure the string is not malicious, and output the associated object.

For example:

import ast

games = ["['football','cricket']"]

ast.literal_eval(games[0])

Output:

['football','cricket']
S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • 2
    This is a good and quite general solution. +1 from me – nikeros Sep 11 '22 at 21:40
  • This is really easy and to the point answer. I'm really thankful to guys like you who are here to help newbie like me. – Ahmed Yasin Sep 11 '22 at 21:43
  • 1
    @dummyfirst - My pleasure. We all continue to learn together. – S3DEV Sep 11 '22 at 21:44
  • 2
    What I like about this is: it would work even in more complex scenarios - for example, in nested lists like `"['football','cricket', ['tennis', 'basketball']]"`. Stripping/replacing characters - while it works for the specific example - could be a recipe for disasters. – nikeros Sep 11 '22 at 21:46
  • yeah i tested that and it worked perfectly. I guess i need to study what ast. and how does it perform this job. – Ahmed Yasin Sep 11 '22 at 21:49
  • @dummyfirst - Interesting you should mention the ‘how it works’. I’ve just updated the answer to include a link to the source code so you can study it yourself. – S3DEV Sep 11 '22 at 21:52
1

this should work

a = ["['football','cricket']"]
out = [val.strip("'") for val in a[0].strip("[|]").split(",")]
print(out)
['football', 'cricket']
Lucas M. Uriarte
  • 2,403
  • 5
  • 19
  • Your solution worked but its seems like quite complicated. The upper answer is seems more easy. but I really appreciate you spent time in answering. Thank You. – Ahmed Yasin Sep 11 '22 at 21:42
1

I find it unlikely that a api is sending a non-json string. This string is not json because of the single quotes. If you convert them to double quotes, you have a valid json.

list_games = json.loads(games[0].replace("'",'"'))
TheMaster
  • 45,448
  • 6
  • 62
  • 85