Bottom Page

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):
Quote
#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
Quote
#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
Quote
#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
Quote
#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.
Quote
#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')]
Quote
#7
Thanks for all of the answers guys. Good start for me here on the forum!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to pass multiple values from one sample to nc variable? Baloch 0 83 Jun-01-2020, 09:27 PM
Last Post: Baloch
  Inserting values from multiple lists sqlite azulu 1 252 May-24-2020, 08:40 AM
Last Post: ibreeden
  UnUnloading values from multiple widgets in a container UGuntupalli 3 245 Apr-20-2020, 08:53 PM
Last Post: UGuntupalli
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 306 Apr-10-2020, 02:39 PM
Last Post: buran
  Multiple lambda functions in zipped list not executing psolar 0 155 Feb-13-2020, 12:53 PM
Last Post: psolar
  Randomly assign values in List 1 to a value in List 2 Seneca260 7 782 Dec-16-2019, 12:57 PM
Last Post: BamBi25
  Syntax not exist noorpy 1 214 Nov-14-2019, 09:23 AM
Last Post: perfringo
  How do I map a list of values to specified colors? larkypython 4 395 Nov-05-2019, 09:22 AM
Last Post: larkypython
  Save all values to pandas of multiple classes jenniferruurs 0 258 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  get value if it's exist [python] senait 1 302 Aug-21-2019, 06:47 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)