Earlier quoted context omitted.
Honest question: why would anyone make a list comprehension that's multiple levels deep? Isn't the purpose of comprehensions to provide quick-and-dirty inline for loops, where a full for loop is too verbose?
People who come from math backgrounds often becomes infatuated with complicated list comprehensions when they realize that they can be used as set-builder notation. {(x,y) for x in range(10) for y in range(10) if y == 2*x}
Asterisks in Python
51–60 of 110 posts
Re: Asterisks in Python
#52Earlier quoted context omitted.
Honest question: why would anyone make a list comprehension that's multiple levels deep? Isn't the purpose of comprehensions to provide quick-and-dirty inline for loops, where a full for loop is too verbose?
It's useful for working with permutations. E.g, [f(a, b) for a in some_list for b in some_list] or even [f(a, b) for a in some_list for b in some_list if a is not b]
import itertools as it
[f(a, b) for a, b in it.product(some_list, some_list) if a is not b]Re: Asterisks in Python
#53I've been programming with Python for over a decade. I mostly understand, but I do try to avoid when possible for maximum clarity. Expanding function variables is fine and clear enough, but multiple levels deep in a comprehension and it can get pretty thick to try and keep it all straight. This article is nice that it covers all the patterns I've seen.
Honest question: why would anyone make a list comprehension that's multiple levels deep? Isn't the purpose of comprehensions to provide quick-and-dirty inline for loops, where a full for loop is too verbose?
citiesByState = {
'WA': ['Seattle', 'Spokane', 'Olympia'],
'OR': ['Portland', 'Salem'],
'CA': ['San Francisco', 'Los Angeles', 'San Diego'],
}
cityToState = {city: state
for state, cities in citiesByState.items()
for city in cities}
I would generally avoid multiple levels in a comprehension but there are some very simple two-level cases like this that I use occasionally. I feel that the code is easy to read, at least.Re: Asterisks in Python
#54Earlier quoted context omitted.
In python 3.6 and up, that becomes def example_cheater(color, flavor, age): return f'I am {age} years old and I like to wear {color} hats and eat {flavor} icecream' Even nicer!
Taken straight out of Ruby, I see. Very convenient feature (and one of my favorites about Ruby), though it does contradict the do-it-one-way mentality.
It'd be interesting to see if there are any older languages that supported it too.
https://rosettacode.org/wiki/String_interpolation_(included)...
Re: Asterisks in Python
#55So this is like the rest and spread operator in javascript (`...`). An interesting observation is that while python has separate operators for lists ( * ) and dictionaries ( * * )—javascript has only `...`. > You need to be careful when using multiple times though. Functions in Python can’t have the same keyword argument specified multiple times, so the keys in each dictionary used with must be distinct or an excepti…
>>> {{'a': 1}, {'a': 2}} {'a': 2}
So the multiple keyword argument problem isn't an inherent issue with so much as it's a problem with keyword arguments being specified twice (which has been a restriction since before they allowed multiple to be used).
Re: Asterisks in Python
#56Side note... Why does everything needs to be bold on that page? It made reading it very difficult.
Side note: I typically bold many sentences in my articles, but this is the first article in a while where I actually bolded nearly no words at all. I wonder what my other articles look like in your browser.
Re: Asterisks in Python
#57Earlier quoted context omitted.
Now how much nicer would it be if even more things were functions, rather than special syntax like here? This can be done with an `apply`.
It is a function - print() is being called as a function, taking a number of positional, arguments followed by named argument. I don't think you're correct about this being special syntax, except for the '*' is being used to expand a list into the positional arguments.
Re: Asterisks in Python
#58Re: Asterisks in Python
#59I've been programming with Python for over a decade. I mostly understand, but I do try to avoid when possible for maximum clarity. Expanding function variables is fine and clear enough, but multiple levels deep in a comprehension and it can get pretty thick to try and keep it all straight. This article is nice that it covers all the patterns I've seen.
I never understand why people feel the need to find shortcuts. It’s like reading smthg[i++] in C. It’s not clear and it could be clearer if written over two lines instead but yet everyone does it.
Re: Asterisks in Python
#60So this is like the rest and spread operator in javascript (`...`). An interesting observation is that while python has separate operators for lists ( * ) and dictionaries ( * * )—javascript has only `...`. > You need to be careful when using multiple times though. Functions in Python can’t have the same keyword argument specified multiple times, so the keys in each dictionary used with must be distinct or an excepti…
Same with Golang: A := []int{1,2} B := append(A, A...) fmt.Println(B) > [1, 2, 1, 2]