0

I've got the following code;

#!/usr/bin/python

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]
list3 = []


def check_match(l1,l2):
    for i in l1:
        if type(i) is list:
            check_match(i,l2)
        for j in l2:
            if type(j) is list:
                check_match(l1,j)
            else:
                if i == j:
                    list3.append(i)

print(list3)

The above code returns an empty list []

Essentially, what i'm trying to do is create a 3rd list which will contain only the unique values, which should look like this;

list3 = [1,2,3,4,5,6,7,8,9,14]

If anyone can guide me, that would be great.

Thanks guys

0

5 Answers 5

2

I suggest using recursion to flatten your lists and then using a set:

def flatten(s, target=int): 
   if type(s) == target:
       yield s
   else:
      for i in s:
         for b in flatten(i):
            yield b

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]
final_result = list(set([*list(flatten(list1)), *list(flatten(list2))]))

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 14]
Sign up to request clarification or add additional context in comments.

Comments

1

Are you looking for something like this ?

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]


no_dub=[]
def checker(lst):
    if not lst:
        return 0
    else:
        for i in lst:
            if isinstance(i,list):
                for k in i:
                    if k not in no_dub:
                        no_dub.append(k)

            else:
                if i not in no_dub:
                    no_dub.append(i)

            return checker(lst[1:])

checker(list1)
checker(list2)
print(no_dub)

output:

[1, 2, 3, 6, 4, 5, 7, 14, 8, 9]

If you don't want to pass one by one then :

check_all=[list1,list2]
for i in check_all:
    checker(i)

print(no_dub)

output:

[1, 2, 3, 6, 4, 5, 7, 14, 8, 9]

Update as per request :

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]


no_dub=[]
def checker(lst):
    if not lst:
        return 0
    else:
        for i in lst:
            if isinstance(i,list):
                for k in i:
                    if "ID{}".format(k) not in no_dub:
                        no_dub.append("ID{}".format(k))

            else:
                if "ID{}".format(i) not in no_dub:
                    no_dub.append("ID{}".format(i))

            return checker(lst[1:])

check_all=[list1,list2]
for i in check_all:
    checker(i)

print(no_dub)

output:

['ID1', 'ID2', 'ID3', 'ID6', 'ID4', 'ID5', 'ID7', 'ID14', 'ID8', 'ID9']

9 Comments

once last request, what's the best way to append a string before the value of i so that the output reads; [ID1, ID2, ID3, ID6, ID4, ID5, ID7, ID14, ID8, ID9] Thanks mate
@ik_tech check the updated solution , I have edited the answer.
@ik_tech Or once you get the list3, you can do ['ID'+str(e) for e in list3]. This will merge ID in front of each element in the list.
@FatihAkici why additional loop when we can do this via same loop ?
thanks guys - any benefit of the flattening as opposed to the solution posted by Aydohyankit?
|
1

You first need to make flat list out of list1 and list2, make an extended list which contains both elements of list1 and list2 and cast the resulting list to set to remove duplicate elements.

Solution i am giving is little less verbose and involve less number of lines

#Flatten list function
flatten=lambda l: sum(map(flatten,l),[]) if isinstance(l,list) else [l]
list1 = flatten(list1) #[1, 1, 1, 2, 2, 1, 2, 3, 6, 3, 4, 5]
list2 = flatten(list2) #[1, 1, 6, 7, 7, 14, 8, 9]
list1.extend(list2) #[1, 1, 1, 2, 2, 1, 2, 3, 6, 3, 4, 5, 1, 1, 6, 7, 7, 14, 8, 9]
list(set(list1))
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 14]

Comments

0

You can make a function that flattens a list, and then convert the result to a set():

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]

def flatten(lst):
    flattened = []
    for item in lst:
        if isinstance(item, list):
            flattened.extend(flatten(item))
        else:
            flattened.append(item)

    return flattened

print(sorted(list(set(flatten(list1 + list2)))))

Which Outputs:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 14] 

Comments

0

I would also suggest flattening and then using set, but saw almost identical answers when I finished coding. Nevertheless, this is how I would do it:

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]
l = list1+list2
flatList = []
for e in l:
    if type(e) == list:
        flatList.extend(e)
    else:
        flatList.append(e)
list(set(flatList))

Result:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 14]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.