remove curly brackets from a list of sets in python

3108 views python
-4

my code looks something like this:

documents=set()
finals = []
temporary_set= set()
temporary_set.add(i)
finals.append(documents.intersection(temporary_set))

when i want to get all values from the finals list i use:

for final in finals:
    print (final)

This returns however the items as a set item within a list. like this:

[{27309053}, {23625724}, {25051134}]

How can i make it that the curly brackets will be omitted and that my result will look like this:

[27309053, 23625724, 25051134]

???

answered question

2 Answers

7

You can flatten your list of sets using itertools.chain.from_iterable:

import itertools

x = [{27309053}, {23625724}, {25051134}]

list(itertools.chain.from_iterable(x))
# [27309053, 23625724, 25051134]

posted this
10

l = [{27309053}, {23625724}, {25051134}]

[x for sl in l for x in sl]  # evaluates to [27309053, 23625724, 25051134]

posted this

Have an answer?

JD

Please login first before posting an answer.