30


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.

Alexandre Deschamps
  • 1,178
  • 2
  • 14
  • 21
  • possible duplicate of [Python's list comprehension vs .NET LINQ](http://stackoverflow.com/questions/3925093/pythons-list-comprehension-vs-net-linq) – nawfal Jul 21 '14 at 19:22

3 Answers3

42
[my_object.my_property for my_object in my_collection]
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
21

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

Ian Henry
  • 22,255
  • 4
  • 50
  • 61
0

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]