Home > Blockchain >  Deleting braces from array
Deleting braces from array

Time:01-29

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

Try:

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

Outputs:

['Python', '5', 'Golang', '4', 'PHP', '3']

CodePudding user response:

You can use a generator:

>>> list(s.pop() for s in data)
['Python', '5', 'Golang', '4', 'PHP', '3']

CodePudding user response:

This code deletes the curly braces.

data = [str(item).replace("{'", "").replace("'}", "") for item in data]

The output looks like this:

['Python', '5', 'Golang', '4', 'PHP', '3']
  •  Tags:  
  • Related