0

I have a list like this;

a=[['2019', '08'], ['2018', '10'], ['2019', '08'], ['2019', '08'], ['2018', '10'], ['2019', '02']]

How can I delete duplicates.

[['2019', '08'], ['2018', '10'], ['2019', '02']]
1

5 Answers 5

2

If order is important (but algorithmic complexity is not):

b = []
for x in a:
    if x not in b:
        b.append(x)

If the complexity is relevant, here's an O(𝑛) solution:

seen = set()
b = []
for x in a:
    t = tuple(x)
    if t not in seen:
        b.append(x)
        seen.add(t)
Sign up to request clarification or add additional context in comments.

Comments

1

You can easily do this with

a=[['2019', '08'], ['2018', '10'], ['2019', '08'], ['2019', '08'], ['2018', '10'], ['2019', '02']]
uniq = []
[uniq.append(x) for x in a if x not in uniq]

uniq
>>[['2019', '08'], ['2018', '10'], ['2019', '02']]

Comments

0

obviously if you don't know this Method. I highly recommand you start with this Implementation and get to know how this work and then move on to higher level implementation

a=[['2019', '08'], ['2018', '10'], ['2019', '08'], ['2019', '08'], ['2018', '10'], ['2019', '02']]

print(a)
b = []
for l in a:
    if l not in b:
        b.append(l)

print(b)

try to play here and get to know how this work because it is a simple Implementation which shows the basics of how the work should get done solving a simple problem like this

Comments

0

try:

a=[['2019', '08'], ['2018', '10'], ['2019', '08'], ['2019', '08'], ['2018', '10'], ['2019', '02']]

a = set(tuple(a) for a in a)
a = [list(a) for a in a]

print(a)
# output : [['2018', '10'], ['2019', '02'], ['2019', '08']]

Comments

-1
a = [['2019', '08'], ['2018', '10'], ['2019', '08'], ['2019', '08'], ['2018', '10'], ['2019', '02']]
a=[list(t) for t in set(tuple(element) for element in a)]
print(a)

Convert elements to tuples then back to a list

Output:

[['2019', '02'], ['2018', '10'], ['2019', '08']]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.