Python's equivalent of C# LINQ's select

Question:

I’m quite new to python, and happen to have used C# for some time now. I saw that there was a filter method to use with the collections, which seems to be the equivalent of the LINQ’s where clause.
I wondered, is there also an equivalent for the LINQ’s select statement in python?
Example: my_collection.select(my_object => my_object.my_property) would return a collection of the my_property of each object in my_collection.

Answers:

You can use map(), but List Comprehensions are a more “pythonic” way of doing this.

Answered By: Ian Henry
[my_object.my_property for my_object in my_collection]

try pandas!
select
C# my_collection.Select(my_object => my_object.my_property)
pandas my_collection['my_property']
or:
C# my_collection.Select(x => x.my_property + 2)
python my_collection['my_property'].apply(lambda x: x + 2)
where
C#: my_collection.Where(x => x.my_property == 1)
pandas: my_collection[my_collection['my_property']==1]

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.