How to "flatten" a list of lists of integers?

Asked

Viewed 110 times

5

I have a list of lists

d = [[1],[2],[3],[3]] 

And I’d like to turn it into a list

s = [1,2,3,3]

I don’t really understand the difference between d and s, but I need to do the transformation because I can use the command set(s) in s, but I can’t use in d. I want to use set(d) because I need a collection without repetitions.

How I transform d in s?

3 answers

6


To get the concatenated output of an iterable use the method chain.from_iterable() available in the module itertools.

from itertools import chain

d = [[1],[2],[3],[3]]

print(set(chain.from_iterable(d)))  #{1, 2, 3}

Test the example on ideone.

Or use chain() unpacking the list with the operator *.

from itertools import chain

d = [[1],[2],[3],[3]]

print(set(chain(*d)))               #{1, 2, 3}

Test the example on ideone.

As for the difference between:

  • s = [1,2,3,3]
  • d = [[1],[2],[3],[3]]

The list in s is a list composed of integers while the list in d is a list composed of lists composed of integers.

  • Good answer, Augusto! I was only in doubt because I am not - shall we say - proficient in Python. But deducing from the behavior of the JS (which is the language I know best), the set does not remove the [3] duplicate because every sub list literally created there has a different address (i.e. reference), right? :-)

  • 1

    @Luizfelipe: In python one of the purposes of set() is to remove duplicate members from iterable. Already the chain() it behaves analogously to javascript Array.prototype.flat().

6

To complement the answers, here are two alternatives I found in the Stack Overflow in English: How to make a flat list out of list of lists?

1. Using sum

d = [[1],[2],[3],[3]]
flattened_list = sum(d, [])

This implementation directly utilizes monoid properties, but may not be very efficient for large lists.

2. Using to reduce

from functools import reduce
import operator

d = [[1],[2],[3],[3]]
flattened_list = reduce(operator.concat, d)

This version also uses a functional dialect (reduction), and is an alternative to implementing with chain of answer by Augusto Vasques.

See solutions working in ideone.com

5

To transform d in s you can implement two loops of nested repeats. In this case the code would be:

d = [[1], [2], [3], [3]]
s = list()
for i in d:
    for j in i:
        s.append(j)

print(s)

In this code the first for scroll through the list d and then the second for goes through each of the lists that are inside the list d. Later the list containing the values will be displayed.

Now if you prefer to use more compact and concise code, you can use List Comprehension and assemble the following code:

Or...

d = [[1], [2], [3], [3]]
s = [j for i in d for j in i]
print(s)

Now, if you intend to generate a single list, you can use the following code:

d = [[1], [2], [3], [3]]
s = list({j for i in d for j in i})
print(s)

See further explanations in the documentation of set.

Running this last code we get as a result:

[1, 2, 3]

Another thing, like the set does not guarantee an ordering of the elements, we can implement the following code:

d = [[1], [2], [3], [3]]
s = sorted({j for i in d for j in i})
print(s)

In this last code I do not need to invoke the function list(), right, the function sorted() min already returns a list with all elements in ascending order.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.