Find Common Elements in 2 list
 Find Common Elements in 2 list quest Silly Frenchman Posts: 44 Threads: 22 Joined: Nov 2020 Reputation: Apr-14-2021, 03:17 PM Hello I have this list: A [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)] B [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)] I want to find number of same elements in the list I tried these methods but the result is always zero: ```list1_as_set = set(A) intersection = list1_as_set.intersection(B) intersection_as_list = list(intersection)```I also tried that one: `commonalities = set(A) - (set(A) - set(B))`But the result is empty .. How can I find the common elements in 2 different list? Reply klllmmm Wafer-Thin Wafer Posts: 88 Threads: 30 Joined: Oct 2016 Reputation: Apr-14-2021, 03:29 PM Hope this helps ```A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)] B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)] list(set(A).intersection(B))`````Output:[(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (25.0,), (22.5,)]`` Reply Larz60+ aetate et sapientia Posts: 10,326 Threads: 382 Joined: Sep 2016 Reputation: Apr-14-2021, 03:31 PM ```>>> A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)] >>> B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)] >>> set(A).union(set(B)) {(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (7.5,), (42.5,), (25.0,), (22.5,)} >>> >>> >>> # With lists better formatted: >>> A = [7.5, 17.5, 22.5, 25.0, 27.5, 30.0, 37.5, 40.0] >>> B = [17.5 22.5, 25.0, 27.5, 30.0, 37.5, 40.0, 42.5] >>> set(A).union(set(B)) {37.5, 7.5, 40.0, 42.5, 17.5, 22.5, 25.0, 27.5, 30.0} >>>``` Reply quest Silly Frenchman Posts: 44 Threads: 22 Joined: Nov 2020 Reputation: Apr-14-2021, 03:33 PM (Apr-14-2021, 03:29 PM)klllmmm Wrote: Hope this helps ```A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)] B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)] list(set(A).intersection(B))`````Output:[(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (25.0,), (22.5,)]`` I have still empty list as a result ... Reply quest Silly Frenchman Posts: 44 Threads: 22 Joined: Nov 2020 Reputation: Apr-14-2021, 03:57 PM (Apr-14-2021, 03:29 PM)klllmmm Wrote: Hope this helps ```A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)] B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)] list(set(A).intersection(B))`````Output:[(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (25.0,), (22.5,)]``It is ok now THanks! klllmmm likes this post Reply

 Possibly Related Threads… Thread Author Replies Views Last Post I cannot delete and the elements from the list quest 4 148 Yesterday, 12:01 PM Last Post: perfringo List of lists - merge sublists with common elements medatib531 1 161 May-09-2021, 07:49 AM Last Post: Gribouillis Sorting Elements via parameters pointing to those elements. rpalmer 3 388 Feb-10-2021, 04:53 PM Last Post: rpalmer Group List Elements according to the Input with the order of binary combination quest_ 19 934 Jan-28-2021, 03:36 AM Last Post: bowlofred List of error codes to find (and count) in all files in a directory tester_V 8 599 Dec-11-2020, 07:07 PM Last Post: tester_V Find _ in a list 1234 15 951 Dec-01-2020, 07:53 PM Last Post: bowlofred Adding List Element if Second part of the List Elements are the Same quest_ 3 417 Nov-25-2020, 04:33 PM Last Post: bowlofred Remove specific elements from list with a pattern Xalagy 3 574 Oct-11-2020, 07:18 AM Last Post: Xalagy Help accessing elements of list of dictionaries Milfredo 6 780 Sep-07-2020, 01:32 AM Last Post: Milfredo Trying to find first 2 letter word in a list of words Oldman45 7 883 Aug-11-2020, 08:59 AM Last Post: Oldman45

Forum Jump:

User Panel Messages

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