How can I do assignments in a list comprehension?

Python 3.8 will introduce Assignment Expressions. It is a new symbol: := that allows assignment in (among other things) comprehensions. This new operator is also known as the walrus operator. It will introduce a lot of potential savings w.r.t. computation/memory, as can be seen from the following snippet of the above linked PEP (formatting adapted … Read more

List comprehension without [ ] in Python

The other respondents were correct in answering that you had discovered a generator expression (which has a notation similar to list comprehensions but without the surrounding square brackets). In general, genexps (as they are affectionately known) are more memory efficient and faster than list comprehensions. HOWEVER, it the case of ”.join(), a list comprehension is … Read more

Line continuation for list comprehensions or generator expressions in python

[x for x in (1,2,3) ] works fine, so you can pretty much do as you please. I’d personally prefer [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long] The reason why \ isn’t appreciated very much is that it appears at the end of a line, where it either doesn’t stand out or needs extra padding, which has … Read more

List comprehension rebinds names even after scope of comprehension. Is this right?

List comprehensions leak the loop control variable in Python 2 but not in Python 3. Here’s Guido van Rossum (creator of Python) explaining the history behind this: We also made another change in Python 3, to improve equivalence between list comprehensions and generator expressions. In Python 2, the list comprehension “leaks” the loop control variable … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)