Python Forum
Use one list as search key for another list with sublist of list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use one list as search key for another list with sublist of list
#1
Hello,

I have two lists that returns some vector values with it's own name:
list1 = [['orange', [1, 2, 3]]]
list2 = [['apple', [1, 2, 3]], ['banana', [1, 2, 3]], ['pear', [-1, 2, 3]], ['kiwi', [-1, -2, 3]]].

I would need to use the list1 as a "search key" and ignoring the string on the list2 and print out the sublist that are not the same than in list1, which in theory should be:
newlist = [['pear' [-1, 2, 3], ['kiwi', [-1, -2, 3]]]

Could anyone give me a light on this?
Reply
#2
You could do
S = set(tuple(x[1]) for x in list1)
result = [x for x in list2 if tuple(x[1]) not in S]
jc4d likes this post
Reply
#3
Thank you for your time and answer Smile
I wanted to go further and then compare the first item of the vector's list only so I tried the code bellow but I get a TypeError:

S = set(tuple(x[1]) for x in list1[0])
result = [x for x in list2[0] if tuple(x[1]) not in S]
Reply
#4
I tried disecting it but is not taking me far since eventhough it is returning the difference, the result is not "filling back" to what list it belongs, which should return
['pear', [-1, 2, 3]]
list1 = [['orange', [1, 2, 3]]]
list2 = [['apple', [1, 2, 3]], ['banana', [1, 2, 3]], ['pear', [-1, 2, 3]], ['kiwi', [1, -2, 3]]]

S = set(tuple(x[1]) for x in list1)

a = [x[1][0] for x in list1]

b = [x[1][0] for x in list2]

z = [x for x in b if x not in a]
print(z)
Reply
#5
I got help on other site but I wanted to share the solution just in case anyone else has the same problem.

list1 = [['orange', [1, 2, 3]]]
list2 = [['apple', [1, 2, 3]], ['banana', [1, 2, 3]], ['pear', [-1, 2, 3]], ['kiwi', [1, -2, 3]]]

y = [x for x in list2 if x[1][0] not in [a[1][0] for a in list1]]
print(y)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  element in list detection problem jacksfrustration 5 367 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 277 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  PyYAML read list of int zisco 2 313 Apr-02-2024, 12:36 PM
Last Post: zisco
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 190 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  list.sort() returning None SmallCoder14 8 572 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  append str to list in dataclass flash77 6 486 Mar-14-2024, 06:26 PM
Last Post: flash77
  Help with to check an Input list data with a data read from an external source sacharyya 3 410 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  list and operator * akbarza 1 259 Feb-29-2024, 08:10 AM
Last Post: DeaD_EyE
  difference between forms of input a list to function akbarza 6 1,036 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  using > < for tuple , list,... akbarza 3 480 Feb-05-2024, 01:18 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020