views:

113

answers:

4

If you google for "pythonic" you will mostly find the same three examples. There are a lot of questions here on stackoverflow that ask for how this and that can be done in a pythonoic way, so a collection of some nice pythonic code examples would be nice!

+5  A: 

as I wrote in the tag description:

Pythonic is a description of the most idiomatic Python code. Not only does this mean that the code is easy to understand for other programmers, but it is also very often the most efficient way to use Python.

SilentGhost
+1  A: 
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Daenyth
A: 

"Pythonic" just means following common Python idioms.

just follow the Zen Of Python:

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren't special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one-- and preferably only one --obvious way to do it.
  • Although that way may not be obvious at first unless you're Dutch.
  • Now is better than never.
  • Although never is often better than right now.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea -- let's do more of those!
Corey Goldberg
+2  A: 

The question asks for a collection of pythonic codes so I will add some of them that I like because they are using the powerful pythonic operators * and ** :

Transposition using unpacking operator *

Unpacking operator is a very powerful tool that allow us to "unpack" a list. I do not know any equivalent in other languages.

This operator can have very funny and useful applications :

a = [[1,2],[3,4]]
a_transpose = zip(*a)

Dictionnary concatenation using tuple unpacking operator **

Once again, I do not know any equivalent in other languages. Same as above, we can use this operator for many things including dictionnary concatenation :

a = {1:2,2:2}
b = {2:37,3:42}
a = dict(a,**b) # a is now {1:2,2:37,3:42}
Elenaher