How do you remove duplicates from a 2d list in python?

This is my solution, I've left the a[i][0] intentionally this way so you can change the member regarding your need.

ab= [['2.71.122.116', 'test_sys_-fw.test_sys_.us'],
     ['10.10.100.26', 'test_sys_5k1'],
     [None, 'Azure'],
     [None, 'test-server'],
     ['2.71.122.119', 'asa-5506-fw'],
     ['33.151.18.23', 'netscaler1'],
     ['33.151.18.23', 'netscaler2'],
     ['33.151.18.23', 'Palo Alto'],
     ['33.151.18.23', 'Arbor CP'],
     ['44.221.2.100', 'fw-la5515'],
     ['44.221.2.101', 'fw-la2-5515'],
     ['44.221.2.99', 'NexusLA2'],
     ['44.221.2.103', 'ASALA5510'],
     ['2.71.122.120', 'asa-5506-fw2'],
     ['2.71.122.106', '2928_SW2']]

def deduplicate_by_ip[a]:
    """
    Clears Empty ip address records from list
    removes duplicates by
    :param a:
    :return:
    """

    source_ips = []
    new_list = []
    for i in range[len[a]]:
        if a[i][0] != None:
            if a[i][0] not in source_ips:
                source_ips.append[a[i][0]]
                new_list.append[a[i]]
    return new_list

list = deduplicate_by_ip[ab]
print["Total items in original list :", len[ab]]
print["Total items after deduplication :", len[list]]
print["The list", list]

The task of removing duplicates many times in the recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let’s discuss certain ways in which this task can be achieved.

Method #1 : Using sorted[] + set[] 

This particular problem can be solved using the above functions. The idea here is to sort the sublist and then remove the like elements using the set operations which removes duplicates. 

Python3

test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],

                           [1, 2, 3], [3, 4, 1]]

print["The original list : " + str[test_list]]

res = list[set[tuple[sorted[sub]] for sub in test_list]]

print["The list after duplicate removal : " + str[res]] 

Output : 

The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [[-1, 0, 1], [1, 3, 4], [1, 2, 3]]

Method #2: Using set[] + map[] + sorted[] 

The task performed by the list comprehension in the above method can be modified using the map function using lambda functions to extend the logic to each and every sublist. 

Python3

test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],

                           [1, 2, 3], [3, 4, 1]]

print["The original list : " + str[test_list]]

res = list[set[map[lambda i: tuple[sorted[i]], test_list]]]

print["The list after duplicate removal : " + str[res]]

Output : 

The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [[-1, 0, 1], [1, 3, 4], [1, 2, 3]]

Method #3 : Using sorted[] method ,in, not in operators

Python3

test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],

                        [1, 2, 3], [3, 4, 1]]

print["The original list : " + str[test_list]]

res1 = []

for i in test_list:

    x=sorted[i]

    res1.append[x]

res=[]

for i in res1:

    if tuple[i] not in res:

        res.append[tuple[i]]

print["The list after duplicate removal : " + str[res]]

Output

The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [[-1, 0, 1], [1, 2, 3], [1, 3, 4]]


How do you remove duplicates from a list in Python?

How to Remove Duplicates From a Python list?.
Method 1 - Naive Method. ... .
Method 2 - Using List Comprehension. ... .
Method 3 - Using set[] ... .
Method 4 - Using list comprehension + enumerate [] ... .
Method 5 - Using collections..

How do I remove duplicates from a list?

Approach:.
Get the ArrayList with duplicate values..
Create a LinkedHashSet from this ArrayList. This will remove the duplicates..
Convert this LinkedHashSet back to Arraylist..
The second ArrayList contains the elements with duplicates removed..

How do you remove duplicates from a matrix in Python?

Method : Using loop This task can be performed in brute force manner using loops. In this, we just iterate the list of list using loop and check for the already presence of element, and append in case it's new element, and construct a non-duplicate matrix.

How do I remove duplicate triplets from a list in Python?

Let's discuss certain ways in which this task can be achieved..
Method #1 : Using sorted[] + set[].
Method #2: Using set[] + map[] + sorted[].
Method #3 : Using sorted[] method ,in, not in operators..

Chủ Đề