-1

I am trying to convert a set inside a list like

x = [set(['Halo', 'Bye'])]

into a list:

['Halo', 'Bye']

However when I typed list(x), the result still shows

[set(['Halo', 'Bye'])]

Is there a way to do this?

I have been looking at various Stackoverflow resources like this and this for a solution but nothing works.

Community
  • 1
  • 1
Fxs7576
  • 1,259
  • 4
  • 23
  • 31

2 Answers2

2

x is a list already, but the set you that are trying to convert is an element of the list x.
So, do:

print (list(x[0]))

instead of just list(x) as the set is the first and the only element in the list x.

lycuid
  • 2,555
  • 1
  • 18
  • 28
0
[item for set_ in x for item in set_]

This will flatten a list of sets (or a list of lists) into just a list

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96