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.
Asked
Active
Viewed 1.6k times
25
Alexandre Deschamps
- 1,128
- 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 Answers
36
[my_object.my_property for my_object in my_collection]
Ignacio Vazquez-Abrams
- 740,318
- 145
- 1,296
- 1,325
-
6Just want to mention, for anybody finding this, the square brackets are necessary. – André C. Andersen Mar 01 '14 at 19:15
18
You can use map(), but List Comprehensions are a more "pythonic" way of doing this.
Ian Henry
- 21,875
- 4
- 48
- 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]
Максим Попов
- 1
- 1