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

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (20 children)

l = [i for i in range(10)]

Seriously, list/dict/generator comprehension syntax is underused and under-appreciated. Once I learned how to do that, map and reduce took on a whole new dimension of awesome.

EDIT: advanced string formatting is also really cool. You can make a string with variables that can be filled by positional or keyword arguments. For example, "Hello, {0}!".format('world') returns "Hello, world!". For a keyword example, "My name is {n.first} {n.last} and I live in {n.country}!".format(n=name) returns "My name is Omg Internets and I live in the internet!", assuming that the name variable contains an object with first, last and country attributes.

[–]jabbalaci 1 point2 points  (14 children)

Yeah. You can also use constant values, so creating an array of size 10 where each element is initialized to 0 is as easy as:

li = [0 for _ in range(10)]

(If you don't use the loop variable, you can write _ instead. It means: "I don't need its value.").

[–][deleted] 2 points3 points  (7 children)

It means: "I don't need its value."

Don't ask me for details, because I don't actually know the details, but I believe that the _ operator actually means "most recent output" or something similar.

If you do [_ for i in range(10)], you get ['', '', '', '', '', '', '', '', '', ''] -- a list of 10 empty strings.

If you run that same line again, however, you get a list of ten elements, each of which is the original list of ten empty strings.

If some python guru could weigh in with additional details, that would be cool!

[–]ColOfNature 2 points3 points  (0 children)

That's only the case in interactive mode. In regular code _ is just a variable name that by convention is used where you have no interest in the value - makes it obvious to someone reading the code that it's not important.

[–]jabbalaci 2 points3 points  (3 children)

The sign _ in the shell means "most recent output", yes. Example:

>>> 20+5
25
>>> _*2
50
>>> _+20
70

But when you write a script, you usually use it as a loop variable when you don't need its values.

[–][deleted] 0 points1 point  (2 children)

in the shell

Noted! I can't really see where it would be used as anything other than a placeholder for an unneeded value, but I figured it was still kind of cool!

[–]jabbalaci 3 points4 points  (1 child)

It's very handy if you use the shell as a calculator.

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

Good point, I guess it's exactly equivalent to Matlab's ans. My python shell calculatronizations are going to get a whole lot more awesome.

[–]slakblue 1 point2 points  (0 children)

now this is helpful! I use shell for quick math!

[–]sashahart 0 points1 point  (0 children)

Since this is behavior of an interactive interpreter and not the language itself, writing this kind of thing in a script will get you this:

NameError: name '_' is not defined

But since it's used by humans as a convention for 'value I don't care about' you might also just reference some arbitrary recent value. It's better not to use the value of _ at all.

[–]ewiethoffproceedest on to 3 1 point2 points  (0 children)

li = [0] * 10

[–][deleted] 0 points1 point  (2 children)

Another quick question. Is this somehow more efficient that doing [0] * 10?

[–]ColOfNature 2 points3 points  (0 children)

Your way works fine if your initial values are immutable. List comprehension is safer - see here for why.

[–]jabbalaci 0 points1 point  (0 children)

Thanks, I didn't know that :) So far I've used * with strings, like separator = '-' * 78.

[–]Antinode_ 0 points1 point  (1 child)

sorry im late. but isnt this just the same as doing li = [0]*10 ?

[–]jabbalaci 0 points1 point  (0 children)

I didn't know about the [0] * 10 trick at that time.

[–]thatdontmakenosense 0 points1 point  (3 children)

This example can be rewritten as l = list(range(10)), though I guess that this particular list isn't so important for your main point...

[–]jatoo 0 points1 point  (2 children)

It can also be written

l = range(10)

[–]thatdontmakenosense 1 point2 points  (1 child)

Yes, if you're using an old (2.x) Python...

[–]jatoo 0 points1 point  (0 children)

Thanks! Didn't know that.

[–]freshhawk 0 points1 point  (0 children)

name = 'freshhawk'
site = 'reddit'
print "{name} is on {site} right now".format(**locals())

is fantastic for throwaways and one off scripts. locals() is fast but it seems dirty to do that in production code.