You are on page 1of 16

30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

1 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

2 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

3 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

4 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

5 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

6 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

7 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

8 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

9 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

10 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

11 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

12 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

13 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

14 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...
56 Comments 1

Jeremy Smith •
Thanks for this. I also liked 1.24, which I hadn't seen before. By the way, here is a good trick that I have
used on a couple of occasions: https://gist.github.com/hrldcp...
• •

Chuntao Lu •
That's one slick trick
• •

chanlvh •
Shouldn't the ToC be at the beginning of the post?
• •

Robb Jones •
Nah, it's right here by the comments where we need it :-D
• •

Wei-Ting Kuo •
For 1.12, we can use
zip(*[iter(a)]*2)
• •

Guest •
or alternatively using dictionary comprehension: {a[k]:k for k in a}
• •

thefourtheye •
This is how it should have been.
• •

Abram Clark •
I also liked 1.24. Totally ingenious :). Also, in 1.30, I think the file reading is just a tidbit cleaner using
with, eliminating the need to close:

with open('contactlenses.csv') as infile:


data = [line.strip().split(',') for line in infile.readlines()]
• •

Paul Scott •
Your unpacking example doesn't demonstrate that you can equally unpack nested tuples:

>>> for i, (key, (left, right)) in enumerate({'a': (0, 1), 'b': (1, 2), 'c': (2, 3)}.items()):
... print i, key, left, right
...
0 a 0 1
1 c 2 3
2 b 1 2
• •

hughdbrown •
And you can use itertools.chain to flatten lists (an alternative to the solution you listed):

>>> from itertools import chain


>>> a = [list(range(4)), list(range(1, 5)), list(range(10,20))]
>>> a
[[0, 1, 2, 3], [1, 2, 3, 4], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]
>>> list(chain(*a))
[0, 1, 2, 3, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
15 de 16 21/06/2016 09:59 a. m.
30 Python Language Features and Tricks You May Not Know About http://sahandsaba.com/thirty-python-language-features-and-tric...

16 de 16 21/06/2016 09:59 a. m.

You might also like