I have a list
like follows:
t=[[1, 7], [3, 7], [1, 7], [5, 8], [3, 7]]
I need to get a set
out of this so the output would be like:
t=[[1, 7], [3, 7], [5, 8]]
I tried to use t = set(t)
but it didn't work.
If you do not care about the order, you can first convert the inner lists
to tuples
using map()
function, and then convert them to set
and then back to list
.
Example -
>>> t=[[1, 7], [3, 7], [1, 7], [5, 8], [3, 7]]
>>> t = list(set(map(tuple,t)))
>>> t
[(3, 7), (5, 8), (1, 7)]
The problem is that, lists
are mutable and thus one cannot make a set
out of them as they might change. Thus you want to use tuples
which are immutable. Thus you can use:
list(set([tuple(ti) for ti in t]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With