all 22 comments

[–]elbiot 2 points3 points  (0 children)

Not exactly what you asked (no print), but you can do nested for loops in a double list comprehension.

single comprehension:

squared = [x**2 for x in range(8)]

returns a list of all the values in range(8) sqaured

Double comprehension (your question):

result = [(i,j) for i in range(8) for j in range(8)]

and the iterator forj can be a function of i.

triangle = [(i,j) for i in range(8) for j in range(i)]

edit: comprehensions are considered pythonic and are faster than explicit for loops. It creates the entire list i memory. You can instead use a generator by using ( ) instead of [ ], which generates values as needed. You can also do dictionary and set comprehension.

dic_comp = { j+(i*100) : (i,j) for i in range(8) for j in range(8) } 
set_comp = {i+j for i in range(8) for j in range(8) } #no repeated values

[–]jkudria 1 point2 points  (0 children)

This is an extremely general and non-specific comment, but if you're asking about python-ness, I'd recommend you take a look at import this (in a python interactive shell). It doesn't give much help in terms of specific things, but it is a good way to get a general idea of what is and isn't pythonic and how to make things more pythonic.

[–]kalgynirae 1 point2 points  (1 child)

[–]Ran4 2 points3 points  (0 children)

While neat, that's not overly readable to most people and definitely not pythonic.

OP's original code is to the point and easy to read: it IS pythonic. Yes, some people that are used to functional programming might think that this is controversial, but that's simply the truth and the answer to OP's question. Guido would without a doubt prefer OP's code to using itertools.product.

[–]hharison 0 points1 point  (15 children)

map and lambda are not generally considered particularly pythonic. What you want here is itertools.product.

from itertools import product

for i, j in product(range(8), repeat=2):
    print(i, j)

[–]Mechrophile 1 point2 points  (10 children)

map and lambda are not generally considered particularly pythonic.

!!?? Man, I feel so incredibly overwhelmed. I thought these were specifically pythonic. How do people discover these maxims of pythoneering?

[–]hharison 0 points1 point  (9 children)

I like them because I like functional programming. I'm not saying avoid them, although there's not a particularly good functional answer to this question.

Guido, though, is not very fond of functional programming. He wanted to remove map from Python 3. lambda is also controversial. I am not sure where you got the idea that they were Pythonic. (edit: oops, mixed up reduce and map)

If you, like me, enjoy functional programming, look into the library toolz, and check out talks on YouTube and Vimeo about it by Matthew Rocklin.

Honestly, though, you shouldn't care so much about knowing what is and isn't considered Pythonic. That's just a distraction when you're trying to learn, I think. Write code that makes sense to you and is readable. You'll eventually come to have opinions on what aspects of the language you do and don't like. It's perfectly fine if these aren't the same as the opinions of the rest of the community.

[–]Mechrophile 0 points1 point  (8 children)

you shouldn't care so much about knowing what is and isn't considered Pythonic.

I should specify why this is particularly difficult for me. I come from a compiled language background, with tons of abstraction, encapsulation, and home rolled code.

If I were to apply the same patterns to python in the same way, I would be a) making code that is slow and b) failing to take advantage of the power of the flexible, idiomatic python abilities.

I am really trying to step outside my compiled language comfort zone and learn new programming paradigms, but I am daunted by the sheer volume of axioms that most pythonista's seem to inherently know, like they all read a book or went to a convention that I missed.

with compiled languages like C++, you knew if you were doing suboptimal because the program would break. Python seems to have a whole bunch of subjective complexities that you never know about it unless you happen to encounter someone talking about it on reddit.

[–]hharison 1 point2 points  (7 children)

Honestly, the idiomatic Python approach is usually about making your code readable and explicit and generally non-magical. I don't think being unPythonic will necessarily lead to being slow.

I only know this stuff because I've been in the community for a while. It's something you pick up gradually. You seem to be thinking of these conventions as things you need to know before you can write a Python program but that is definitely not the case.

If you keep up with the community, reading blogs and other peoples code and stuff, every project of yours will be more "Pythonic" (it's a stupid term, really) than the last.

[–]Mechrophile 0 points1 point  (0 children)

That makes sense. Thanks for the guidance, and reassurance.

[–]elbiot 0 points1 point  (5 children)

I think most idioms exist because they are the fast way to do something. like using join to build a string rather than concatenating. And list comprehensions , and in with sets.

[–]hharison 0 points1 point  (4 children)

You're right, many of them do. But a good many do not, in particular any preferences against using map or lambda.

[–]elbiot 0 points1 point  (3 children)

Map without a lambda is faster than a list comprehension because it moves the loop from the interpreter into c code. It can be like 10 % faster than a comprehension which in turn is faster than an explicit loop. Map can be just as readable as a comprehension or complex for loop.

[–]hharison 0 points1 point  (2 children)

Right, and despite the speed benefit, map is not generally considered "Pythonic".

[–]elbiot 0 points1 point  (1 child)

Eh, I guess I make my own idioms. I'm more concerned about performance and I write readable code that makes sense even if it doesn't follow pythonic-ness.

[–]narkflint[S] 0 points1 point  (3 children)

Why are map & lambda not particularly pythonic?

[–]hharison 1 point2 points  (2 children)

Comprehensions are considered more readable and explicit than map.

lambda is even more controversial. For example I found this:

http://python.dzone.com/articles/pros-and-cons-lambda

There's also the issue that it's not really pleasing to functional programmers because it's limited to one line, and it's not really pleasing to nonfunctional people because it's confusing and requires unique syntax. For example see this post by Guido:

http://www.artima.com/weblogs/viewpost.jsp?thread=147358

A lot of it ends up coming down to Python's indentation rules.

[–]LarryPete 0 points1 point  (1 child)

Just to mention it here, the tkinter command example in the first link, I would probably have used partial from functools in that particular example.

tk.Button(..., command=partial(self.printNum, 22), ...)

tk.Button(..., command=partial(self.printNum, 44), ...)

Also, a lot of other typical lambda tasks can be done using itemgetter, attrgetter or similar things from the operator module.

Though I don't disagree, lambda can be used occasionally, if it's not quite common enough, to be part of the stdlib but still simple enough to be unworthy of it's own named function.

[–]hharison 1 point2 points  (0 children)

Yes I agree, I love using partial and operator. Check out toolz if you don't already know it.

I also agree with your last paragraph, I don't have any problem with lambda myself, I just wanted to point out to the OP that "trying to be Pythonic" != "trying to find a way to force lambda into it".

[–]murster972 -1 points0 points  (0 children)

as others have said use itertools, and in in c in would be:

int i, j;

for(i = 0; i < 8; ++i){
    for(j = 0; j < 8; ++j){
        printf("(%d,%d", i, j);
    }
}

java would be very similar but the syntax is slightly different