16
Wisecrack
18d

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.

Comments
  • 1
    I 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.
  • 3
    lol, 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? 😂
  • 4
    List 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
  • 3
    @Wisecrack i thought you were smarter than me :( this is baby stuff
  • 2
    @Lensflare map is just a particular case of reduce. Now I mostly use reduce tbh
  • 1
    Haha, 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?
  • 1
    @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
  • 0
    contain [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.
  • 2
  • 1
    @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.
  • 1
    Now do a walrus operator!
  • 0
  • 1
    @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
    ]
    ```
  • 1
    @JsonBoa the walrus operator pales in comparison to the Elvis operator :)
  • 1
    @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.
Add Comment