Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Demolishun3504459dI was searching list comprehensions about 10 years ago (damn it has been a while) on Google. The Google search opened up like it was 3D and invited me to apply to work there. Apparently this was a way to garner interest of people who were searching for "advanced" (?, no idea) programming concepts. It happened twice and it always python list and dictionary comprehensions.
-
Lensflare1746859dlol, map() became one of the most important tools in my tool-belt in the past decade or so.
It just transforms a list of elements from one kind into another without the bullshit of "for" loops.
How can you hate it? 😂 -
Hazarth952159dList and map comprehensions are awesome, map is awesome, zip is awesome, the entire intertools package is awesome
Just because juniors exist doesn't mean we have to avoid 30% of the language -
antigermgerm39259d@Lensflare map is just a particular case of reduce. Now I mostly use reduce tbh
-
typosaurus1215559dHaha, you just mean [not_horse for animol in stable if animol.kind in ["pony","sheep"] and animal.is_alive]? Heh, nice.
@chatgpt how to comprehend list comprehensions? -
chatgpt55359d@retoor
List comprehensions in Python are a concise way to create lists based on existing lists or other iterable objects. They consist of an expression followed by a for clause, and possibly multiple for or if clauses.
Here's a general template for a list comprehension:
new_list = [expression for item in iterable if condition]
In this template:
- 'expression' is the output you want to generate for each item in the iterable.
- 'item' is the variable that represents each item in the iterable.
- 'iterable' is the object you are iterating over.
- 'condition' is an optional filter that only includes items where the condition evaluates to True.
For example, let's say you have a list of numbers and you want to create a new list containing only the even numbers from the original list. You can do this using a list comprehension:
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers = [num for num in original_list if num % 2 == 0]
After executing this code, the list 'even_numbers' will -
chatgpt55359dcontain [2, 4, 6, 8].
List comprehensions are a powerful and efficient way to manipulate data in Python and are especially useful when you want to create a new list based on a condition or transformation applied to an existing list. -
Wisecrack947858d@antigermgerm in some areas I am smarter. Definitely not programming though.
I've been coding with raw loops this entire time because for the longest time I just couldn't seem to grok fp.
Now I do and oh my god why the fuck would I ever go back. -
JsonBoa301452d@Wisecrack the walrus operator - a twisted thing from the depths of a twisted mind.
Behold!
(Look for the " := " that someone thinks looks like the face of a walrus laying on its side)
```
def square(val):
return val**2
odd_squares = [
sqr
for val in range(10)
if (sqr := square(val)) % 2 != 0
]
``` -
JsonBoa301452d@Lensflare I had never seen the ternary operator called "the Elvis operator". But now I can't unsee it.
Although kids nowadays will most likely call it by the name of some Bollywood bloke.
Related Rants
I FINALLY comprehend list comprehensions.
I can write an unlimited amount of nested loops on a single line and make other less experienced people hate me for fun and profit.
Also learned about map() #I hate it#, zip(which is awesome), and the utility of lambdas (they're okay).
Enumerate is pretty nifty too, only thing I lose is setting the initial value of the iterator index.
rant
functional programming
fp