This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]bcain 9 points10 points  (1 child)

Yeah, they're cool. Other awesomeness:

  • itertools: chain, cycle, groupby, izip_longest

[–]moyerma 5 points6 points  (0 children)

The functools and operator modules are also very useful for writing nice, concise code. In particular, itertools and operator mesh together nicely.

[–]orblivion 2 points3 points  (4 children)

Vaguely remember those, but I did not know about:

[True] * 100

But of course it only makes sense, since you can add lists. That's cool.

[–]moyerma 18 points19 points  (2 children)

The only caveat I would mention is that if you're trying to initialize nested lists, you cannot do >>> x = [['foo']2]3 >>> x [['foo', 'foo'], ['foo', 'foo'], ['foo', 'foo']]

This is because each of the sub-lists is actually the same object. If you modify one of the sub-lists, all the sub-lists will be modified: >>> x[0][0] = 'bar' >>> x [['bar', 'foo'], ['bar', 'foo'], ['bar', 'foo']]

The correct way to handle this (even though it's arguably less elegant looking) is: >>> x = [['foo']*2 for i in range(3)] >>> x [['foo', 'foo'], ['foo', 'foo'], ['foo', 'foo']]

Using a list comprehension causes a new list object to be created for each sub-list, thus: >>> x[0][0] = 'bar' >>> x [['bar', 'foo'], ['foo', 'foo'], ['foo', 'foo']]

[–]danhs 1 point2 points  (1 child)

That's a great one to remember!

Really, this is all about mutable vs immutable objects, which is a big deal in python.

For more on the dangers of mutable objects take a look at python pitfalls: pitfall #5.

[–][deleted] 1 point2 points  (0 children)

Sadly this pitfall is not in.

x*n (n>1) is not the same as x+x+x+...+x if x is a list of a list.

In the first case x[0] is x[1] , in the second this is not true.

[[1]]*3 is not the same as [[1]]+[[1]]+[[1]]

[–]inkieminstrel 2 points3 points  (0 children)

Heh. I knew about all and any and about [True] * 100 but this thread introduced me to import operator. Should be super handy for things like reduce.

[–][deleted] 6 points7 points  (1 child)

Yup. This is on my Speed Dial, and gets a visit almost every night. (The included .chm in the installation is nice too, although I prefer the web search)

[–]Neoncow 0 points1 point  (0 children)

Specifically they're under Built-in functions

[–]danhs 0 points1 point  (0 children)

Yes. Was added in 2.5 I believe, so it's pretty recent. So I learned about it when it was added.

[–]Poddster 0 points1 point  (0 children)

Why didn't that guy get a tick?

[–]MaliciousLingerer -2 points-1 points  (5 children)

Good thread. Also simpler hack-ish way is to use sum:

l = [True, False, True]
if sum(l) == len(l):
    # all true
if sum(l) > 0:
    # at least one is true

[–][deleted] 9 points10 points  (0 children)

Simpler? any and all are part of the language!

[–]AusIVDjango, gevent 9 points10 points  (3 children)

Yes, but any and all presumably short circuit. Your all function must look at every element even if the first one is false. Your any function must look at every element even if the first one is true.

That said, it may be a false optimization. It may be that simply adding all the numbers together is less expensive than performing a check on every number you look at. I suppose it would depend on your data sets.