Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:54:18+00:00 2026-06-10T02:54:18+00:00

In C#, with LINQ, if I have en enumeration enumerable , I can do:

  • 0

In C#, with LINQ, if I have en enumeration enumerable, I can do:

// a: Does the enumerable contain an item that satisfies the lambda?
bool contains = enumerable.Any(lambda);

// b: How many items satisfy the lambda?
int count = enumerable.Count(lambda);

// c: Return an enumerable that contains only distinct elements according to my custom comparer
var distinct = enumerable.Distinct(comparer);

// d: Return the first element that satisfies the lambda, or throws an exception if none
var element = enumerable.First(lambda);

// e: Returns an enumerable containing all the elements except those
// that are also in 'other', equality being defined by my comparer
var except = enumerable.Except(other, comparer);

I hear that Python has a more concise syntax than C# (and is therefore more productive), so how do I achieve the same with an iterable in Python, with the same amount of code, or less?

Note: I don’t want to materialize the iterable into a list if I don’t have to (Any, Count, First).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:54:18+00:00Added an answer on June 10, 2026 at 2:54 am

    The following Python lines should be equivalent to what you have (assuming func, or lambda in your code, returns a Boolean):

    # Any
    contains = any(func(x) for x in enumerable)
    
    # Count
    count = sum(func(x) for x in enumerable)
    
    # Distinct: since we are using a custom comparer here, we need a loop to keep 
    # track of what has been seen already
    distinct = []
    seen = set()
    for x in enumerable:
        comp = comparer(x)
        if not comp in seen:
            seen.add(comp)
            distinct.append(x)
    
    # First
    element = next(iter(enumerable))
    
    # Except
    except_ = [x for x in enumerable if not comparer(x) in other]
    

    References:

    • List comprehensions
    • Generator expressions
    • any() built-in function
    • sum() built-in function
    • set type

    Note that I renamed lambda to func since lambda is a keyword in Python, and I renamed except to except_ for the same reason.

    Note that you could also use map() instead of the comprehensions/generators, but it is generally considered less readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have linq request. I need get item.Title in select. how do this? var
I have a LINQ to Entity query that is running really slow. This query
I have a Linq expression that (for northwind) gets the total qty ordered per
I have some LINQ code that generates a list of strings, like this: var
I have a Linq query that looks something like this: var myPosse = from
I have this linq query that works well (although it may be written better,
If I have an enumeration of dictionaries IEnumerable<IDictionary<string, float>> enumeration can I perform a
I have a function that accepts an Enumerable. I need to ensure that the
I have an enumerable that takes a long time to get the next value.
I have a field in my database table that use to store an enumeration

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.