0

I am trying to delete "{" and "}" from array which looks like:

data = [{'Python'}, {'5'}, {'Golang'}, {'4'}, {'PHP'}, {'3'}]

Solutions like converting array/list to string and after that replacing symbols do not work as expected.

Could you help with it, please?

Im waiting output like: ['Python', '5', 'Golang', '4', 'PHP', '3']

Mykola
  • 9
  • 4
  • What you have there is a list comprised of a number of sets. What output are you hoping to get? – DarkKnight Jan 28 '22 at 17:16
  • 1
    How did the data end up in this format? It'd be better to fix the problem at the source: change the code that's spitting out the data to not have the unnecessary `{...}` sets, instead of patching it up afterwards. – John Kugelman Jan 28 '22 at 17:19
  • 2
    Why do you have a list of sets all containing one item? If they all really contain one item in your real data, then you could just do `[item.pop() for item in data]`, which would turn your list of sets of strings into just a list of strings: `['Python', '5', 'Golang', '4', 'PHP', '3']`. But if you could avoid putting the sets into your list in the first place, and instead just put the strings directly in it, that would make more sense. I get the feeling that this is an "[XY Problem](https://xyproblem.info/)". – Random Davis Jan 28 '22 at 17:19
  • Does this answer your question? [How to make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) – Tomerikoo Jan 28 '22 at 19:46
  • it happened after scraped data through bs4, I mean {...} format of list – Mykola Jan 30 '22 at 14:17

4 Answers4

2

This array looks to be a list of sets with a single element. You can create a new list of strings:

data = [''.join(my_set) for my_set in data]

The output is:

['Python', '5', 'Golang', '4', 'PHP', '3']
Marco Valle
  • 176
  • 6
1

This may not be the most elegant solution, but it works:

new_data = []
for item in data:
   new_data.append(item.pop())
data = new_data
Jakob
  • 58
  • 7
0

Try:

[list(i)[0] for i in data]

Outputs:

['Python', '5', 'Golang', '4', 'PHP', '3']
Multivac
  • 735
  • 6
  • 10
0

You can use a generator:

>>> list(s.pop() for s in data)
['Python', '5', 'Golang', '4', 'PHP', '3']
dawg
  • 98,345
  • 23
  • 131
  • 206