Check if list<t> contains any of another list

check if list is empty python
python check if variable is list or string
python check if (list or tuple)
python check if value exists in list
isinstance list
python if list
python check if iterable
how to check if a tuple is in a list

I have a list of parameters like this:

public class parameter
    public string name {get; set;}
    public string paramtype {get; set;}
    public string source {get; set;}

IEnumerable<Parameter> parameters;

And a array of strings i want to check it against.

string[] myStrings = new string[] { "one", "two"};

I want to iterate over the parameter list and check if the source property is equal to any of the myStrings array. I can do this with nested foreach's but i would like to learn how to do it in a nicer way as i have been playing around with linq and like the extension methods on enumerable like where etc so nested foreachs just feel wrong. Is there a more elegant preferred linq/lambda/delegete way to do this.


You could use a nested Any() for this check which is available on any Enumerable:

bool hasMatch = myStrings.Any(x => parameters.Any(y => y.source == x));

Faster performing on larger collections would be to project parameters to source and then use Intersect which internally uses a HashSet<T> so instead of O(n^2) for the first approach (the equivalent of two nested loops) you can do the check in O(n) :

bool hasMatch = parameters.Select(x => x.source)

Also as a side comment you should capitalize your class names and property names to conform with the C# style guidelines.

Python, Given an object, the task is to check whether the object is list or not. Method #1: Using isinstance. filter_none. edit close. play_arrow. link brightness_4 code� Given an object, the task is to check whether the object is list or not. Method #1: Using isinstance

Here is a sample to find if there are match elements in another list

List<int> nums1 = new List<int> { 2, 4, 6, 8, 10 };
List<int> nums2 = new List<int> { 1, 3, 6, 9, 12};

if (nums1.Any(x => nums2.Any(y => y == x)))
    Console.WriteLine("There are equal elements");
    Console.WriteLine("No Match Found!");

How to check if an object has type list in Python, Use isinstance() to check if an object has type list. Call isinstance(object, class_or_tuple) with class_or_tuple as list to return True if object is an instance or subclass of list and False if otherwise. Check if value exist in list using list.count () function. List.count () function gives the number of occurrences of the value passed as parameter. Here, we used this function in if condition to determine if number of occurence is equal to 0 then the value does not exist in our list else value exist in our list.

If both the list are too big and when we use lamda expression then it will take a long time to fetch . Better to use linq in this case to fetch parameters list:

var items = (from x in parameters
                join y in myStrings on x.Source equals y
                select x)

Python isinstance() explained with examples [Guide], Check if an object is an instance of a specified class. Check whether variable or object is of dict type, list type, set type, tuple type. The isinstance()� Check if element exist in list based on custom logic Python any () function checks if any Element of given Iterable is True. Let’s use it to check if any string element in list is of length 5 i.e.

Python: Test if a variable is a list or tuple or a set, Python Exercises, Practice and Solution: Write a Python program to test if a variable is a list or tuple or a set. You can use the following formula to check if a certain cell value match to a list or not in Excel. 1. Select a blank cell, enter formula =IF(ISNUMBER(MATCH(C2,A2:A7,0)),1,0) into the Formula Bar , and then press the Enter key.

Check If Value Is In List in Excel, How to find if a value in a list. We can check it using a simple formula of COUNTIF function in excel. Let's see how? Python | Check whether a list is empty or not There are various ways in which a list can be checked in Python, but all are not appropriate or in the terms of Python, “pythonic”, to implement. Let’s see how we can check a list is empty or not, in a less pythonic way.

Python: Check if a list or list of lists is empty or not –, Check if a list is empty using “not” operator in python. In python, a sequence object can be implicitly convertible to bool. If sequence is empty� This means that when you do type (tmpDict [key])==list if will return False because they aren't equal. That being said, you should instead use isinstance (tmpDict [key], list) when testing the type of something, this won't avoid the problem of overwriting list but is a more Pythonic way of checking the type.

  • thanks seems to be what im looking for i will try it out. Need to play around with the functional side of things a bit more. regarding capitalising class and properties, i do i just was forgot when wrote the example above.
  • Why O(n^2)? Isn't it O(n*m) as we are talking abount two variables and not one? Since m (the parametes) are a constant, it's the same as O(n). I don't see how intersect should be much faster here? But agreed, Intersect has the potential to be faster, but is not guaranteed.
  • You are right that it should be O(n*m) - m is not a constant though - it is the size of one of the lists, even though in the particular example given it might be "2". Even constant values though are not negligible in practice - for all non-trivial list lengths the Intersect will be faster - if the lists are trivially short, it does not matter one way or another (in that case performance probably is not your concern at all anyway)
  • how can u figure out the list index where the condition becomes true ? I have a list with sentences. I have an array with particular words. I want indexes of the list if the sentence have atleast one words from the array. @BrokenGlass
  • Performance-wise, wouldn't parameters.Any(x => myStrings.Contains(x.source)); be better than your first example?
  • Note that if the lists involved are large, this will end up being a lot slower than the Intersect approach, as it's O(N*M) in the sizes of the lists. (It's O(1) in memory though.)