Python Forum
Check if multiple values exist in a list
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if multiple values exist in a list
#1
I am trying to find a way of testing whether or not at least one element from a list #1 is present in a list #2
One thing I've found is this thread: http://stackoverflow.com/questions/74028...-in-a-list though I dont' really understand the first(accepted) answer. If someone could break that answer down or suggest an alternative way of doing it, that would be a big help. 

It would look like:

mylist = [1,2,3,4]
mylist2 = [4,5,6]


if (any(mylist) in mylist2):
Reply
#2
If the list values are uniques is better to use sets.
http://www.programiz.com/python-programming/set

for value in list1:
    if value in list2:
        print(value, "True")
   else:
       print(value, "False")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
The SO answer is making sets from the lists. In Python a set is a collection without duplicate elements:
>>> set('abracadabra')
set(['a', 'r', 'b', 'c', 'd'])
These sets mimic the mathematical objects of the same name on which you can use the intersection operator to determine the elements that two sets have in common.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
Why this code doesn't work?
print(value, "True") for value in list1 if value in list2
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Oct-06-2016, 07:20 PM)Ofnuts Wrote: The SO answer is making sets from the lists. In Python a set is a collection without duplicate elements:
>>> set('abracadabra')
set(['a', 'r', 'b', 'c', 'd'])
These sets mimic the mathematical objects of the same name on which you can use the intersection operator to determine the elements that two sets have in common.

Therefore, if you want to know whether sets (or lists, turned to sets) have any elements in common, you want to check if intersection of the sets is an empty set or not.
Reply
#6
(Oct-06-2016, 07:28 PM)wavic Wrote: Why this code doesn't work?
print(value, "True") for value in list1 if value in list2
Because it is not valid python code

mylist = [1,2,3,4]
mylist2 = [4,5,6]
print([(value, "True") for value in mylist if value in mylist2])
Output:
[(4, 'True')]
Reply
#7
Thanks for all of the answers guys. Good start for me here on the forum!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 402 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 681 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  __init__() got multiple values for argument 'schema' dawid294 4 2,265 Jan-03-2024, 09:42 AM
Last Post: buran
  Copying the order of another list with identical values gohanhango 7 1,134 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,216 Nov-03-2023, 05:35 PM
Last Post: huzzug
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,680 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Comparing List values to get indexes Edward_ 7 1,138 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  How to check multiple columns value within range SamLiu 2 1,140 Mar-13-2023, 09:32 AM
Last Post: SamLiu
  Adding values with reduce() function from the list of tuples kinimod 10 2,634 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,064 Dec-29-2022, 05:54 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