Python Tips & Tricks of the Trade

March 1st, 2010 (Guest) Leave a comment
Like the article?
Python Logo

Python is a very useful and robust scripting language which has been gaining in popularity lately. Some find it similar to Ruby and others compare it to Java. Python shares some elements of both languages as well as some constructs of C/C++. While new Python programmers are using the language they often build Python code that mimics other languages they’ve used before. However, there is a “Pythonic” way of doing a number of tasks in Python. These Python tips and tricks will help you write code that is not only more “Pythonic” but easier to understand.

Lambda Functions

Lambda functions are one Python trick that can greatly reduce the complexity of your code. A lambda function is defined in-line and is typically a very short function that is not called repeatedly. The function is not bound to a memory location or variable. An example of a lambda function that takes a number and returns the square of that number would be:

lambda x: x*x

Lambda functions are often used with filter, map and reduce. These functions take a function and a list as arguments and return the list as modified by the function. Rather than writing a traditional function and passing it to the filter, map, or reduce methods you can define a lambda function in-line. For example:

list = [1, 2, 3, 4]
squares = map[lambda x: x*x, list]

This function would return [1,4,9,16].

List Comprehension

Another useful Python trick involves list comprehension. In our example of lambda functions, we used map to return the squares of each element of a list. While this is useful, it makes for Python code that isn’t exactly clear. Someone new to Python might not readily notice what this code does. List comprehensions are another way to achieve the same result but it results in code that is much cleaner and on a quick glance, much more obvious. Here’s the same code as above but using our Python trick – list comprehension:

list = [1, 2, 3, 4]
squares = [x*x for x in list]

It is readily obvious to even a Python newbie that we’re making a new list by squaring each element of our original list. This Python trick results in code that is much cleaner and readily understood. You can also use this Python trick to filter a list. For example, to find all the elements of list that are less than 4:

list = [1, 2, 3, 4]
new_list = [x for x in list if x < 4]

List comprehension allows you to easily filter the list. This Python tip results in code that is easier to follow than the traditional filter method.

Conclusion

Python is a great language with a lot of power. Python newbies often miss opportunities to create clean, readable code because they code like they did in other languages in Python. A couple Python tips and tricks such as using lambda functions and list comprehensions can make your code much easier to read and maintain.

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
If you liked this article, consider enrolling in one of these related courses:
Don't miss another post! Receive updates via email!

About Alex Dawson

Alex is a freelance web developer who has by now built a dozen of web applications using the Python and Django combo. He has written several articles on this and other subjects related to e-commerce and web hosting, which can be found on his EzineArticles author profile.

Comment