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.
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? :-)– Luiz Felipe
@Luizfelipe: In python one of the purposes of
set()
is to remove duplicate members from iterable. Already thechain()
it behaves analogously to javascriptArray.prototype.flat()
.– Augusto Vasques