Python – How to collapse the values of a series whose values are a list into a unique list

How to collapse the values of a series whose values are a list into a unique list… here is a solution to the problem.

How to collapse the values of a series whose values are a list into a unique list

Given a Pandas series as follows:

0 [ID01]
1 [ID02]
2 [ID05, ID08]
3 [ID09, ID56, ID32]
4 [ID03]

The goal is to get a single list like this:

[ID01, ID02, ID05, ID08, ID09, ID56, ID32, ID03]

How do I implement it in Python the way Pythonic does?

Solution

Let’s say this is a pandas. Series object

Option 1

Full list

np.concatenate(s).tolist()

Option 1.1

Unique list

np.unique(np.concatenate(s)).tolist()

Option 2

Valid if the element is a list. It doesn’t work if they are numpy arrays.
Full list

s.sum()

Option 2.1

Unique list

pd.unique(s.sum()).tolist()

Option 3

Full list

[x for y in s for x in y]

Option 3.1

Unique list (thanks @pault).

list({x for y in s for x in y})

@Ven’s choice

list(set.union(*map(set, s)))

Settings

s = pd. Series([
    ['ID01'],
    ['ID02'],
    ['ID05', 'ID08'],
    ['ID09', 'ID56', 'ID32'],
    ['ID03']
])

s

0                [ID01]
1                [ID02]
2          [ID05, ID08]
3    [ID09, ID56, ID32]
4                [ID03]
dtype: object

Related Problems and Solutions