Welcome to Smips.com Sign in | Join | Help
Python’s LINQ Equivalents – filter(), map() and list comprehension
I started learning Python this year, and was pleasantly surprised to discover that Python has built-in language support that offers the equivalent of C#’s Language Integrated Query (LINQ).

When LINQ was introduced with C# 3, it instantly became one of my favorite language features. LINQ made code for querying collections easier to read, easier to write, more compact, more clear, and more entertaining. LINQ, in short, is pretty sweet.

But Python has all the goodies of LINQ, too – in the form of the filter() function, the map() function, and list comprehension.  This Python LINQ-ish trio is so nice I don’t find myself missing LINQ at all when writing Python.  But enough of that … let’s see some code.

Let’s say that we have a list of states.  And let’s say we have a state object that holds these states.

   1: public class State
   2: {
   3:     public string Code { get; set; }
   4:     public string Name { get; set; }
   5: }

Here are some exercises using both LINQ and Python to query our list of states.

Get the states that start with “M”

In C#, we have two options for using LINQ: the standard query operators are methods that take lambda expressions and LINQ query syntax is a more elegant abstraction that looks a bit like SQL syntax. Both do the same thing in the end.

LINQ standard query operator methods

   1: var results = states.Where(x => x.Name.StartsWith(“M”));

LINQ query syntax

   1: var results = from s in states
   2:     were s.Name.StartsWith("M")
   3:     select s;

Python also has two options that match up one-for-one with the LINQ options. We have the filter function that takes a lambda expression, and we have list comprehension, a more elegant abstraction. Like our two LINQ examples, both do the same thing in the end.

Python filter() function

   1: results = filter(lambda x : x.name.startswith('M'), states)

Python list comprehension

   1: results = [s for s in states if s.name.startswith('M')]

Make a list of just the state codes

Another common task for LINQ is to make a list of completely different types than the original list contains. Let’s say we want to create a brand-new list that is a list of state codes (strings) instead of a list of state objects.

LINQ standard query operator methods

   1: var results = states.Select(x => x.Code);

LINQ query syntax

   1: var results = from s in states select s.Code;

In this case, Python uses the map() function. map() works much like filter() but is used to transform the original objects instead of filtering some of them out.

Python map() function

   1: results = map(lambda x : x.code, states)

Python list comprehension

   1: results = [s.code for s in states]

Mixing and matching

And of course, we can filter and transform at the same time. These two pieces of code will make a new list of state codes for those states that start with an “M”.

LINQ query syntax

   1: var results = from s in states
   2:     were s.Name.StartsWith("M")
   3:     select s.Code;

Python list comprehension

   1: results = [s.code for s in states if s.name.startswith('M')]

Conclusion

There you have it! All the fun of LINQ, but in Python. And without curly braces. I remember thinking what a revolution LINQ was, and now I wonder if the Python guys were chuckling at me behind my back. Oh well!

You can see the Python code samples in action at codepad: http://codepad.org/0H6wom4T

Share this post: Email it! | bookmark it! | digg it! | reddit!
Posted: Monday, June 01, 2009 5:26 PM by brad
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS