Organize a Python two-dimensional list, excluding duplicate elements


This is a memo of how to remove the duplicate list and put it together in Python two-dimensional list (list in list)

Introduction

Two-dimensional lists that have a duplicate list like this….

(The list is a and b respectively)

print(a)
[['01', '02', '03', '04'], ['11', '12', '13', '14']]
print(b)
['11', '12', '13', '14'], ['21', '22', '23', '24']]

I want to eliminate duplication and add them together as follows.

[['01', '02', '03', '04'], ['11', '12', '13', '14'], ['21', '22', '23', '24']]

But adding a and b normally, the duplication will remain like this. .. ..

[
['01', '02', '03', '04'],
['11', '12', '13', '14'],
['11', '12', '13', '14'],
['21', '22', '23', '24']
]

If this is a one-dimensional list, it can be achieved to remove duplicates with set function and then to convert result to the list, this way is often come up when I google how to remove duplication.

c = ['01', '02', '03', '04', 
     '11', '12', '13', '14', 
     '11', '12', '13', '14',
     '21', '22', '23', '24']
print(set(c))
{'01', '02', '03', '04', '11', '12', '13', '14', '21', '22', '23', '24'}
print(list(set(c))) 
['11', '03', '01', '04', '21', '22', '12', '02', '13', '14', '23', '24']
#The original order is ignored.

Set function doesn’t work for a two-dimensional list (list in list).

So have to do it another way.

After all, using a for loop to manage this.

Set function doesn’t work for two-dimensional list

I will give it a try using the following two-dimensional lists.

a = [
    ['01', '02', '03', '04'],
    ['11', '12', '13', '14']
]

b = [
    ['11', '12', '13', '14'],
    ['21', '22', '23', '24']
]

Merging them normally, they will be combined as they are at the beginning of this post.

merge = a + b
print(merge)
[
['01', '02', '03', '04'],
['11', '12', '13', '14'],
['11', '12', '13', '14'],
['21', '22', '23', '24']
]

Attempting to remove duplicate elements with set function from this two-dimensional list does not work. Wow, I’m sorry….

set(merge)
-----------------------------------------------------------TypeError Traceback (most recent call last) 
<ipython-input-50-7a46158a21e1> in <module> ----> 1 set(merge) TypeError: unhashable type: 'list'

So, another way

I can’t help it, so I’ll do it as follows.

a = [
    ['01', '02', '03', '04'],
    ['11', '12', '13', '14']
]

b = [
    ['11', '12', '13', '14'],
    ['21', '22', '23', '24']
]
#Copy it first
merge = a.copy()

#Append if the element in b is not in a.
for x in b:
    if x not in a:
         merge.append(x)    

print(merge)
[['01', '02', '03', '04'], ['11', '12', '13', '14'], ['21', '22', '23', '24']]

Done!

It’s fine with a following way as well.

#make a list of elements in list b that are not in list a, and add them to list a.
merge = a + [x for x in b if x not in a]
print(merge)
[['01', '02', '03', '04'], ['11', '12', '13', '14'], ['21', '22', '23', '24']]

Well, if the number of elements is small, you may just use remove ().

Of course, there may be a possible way but I don’t know.

For the time, happily ever after.

Environments

Python; 3.7.2
Jupyter ; 1.0.0
macOS; Catalina version 10.15.5


ちょっと広告です
https://business.xserver.ne.jp/

https://www.xdomain.ne.jp/

★LOLIPOP★

.tokyo

MuuMuu Domain!