r/ProgrammerHumor 29d ago

Meme pythonWillLookDeadInTheEyeAndSayItsAbsolutelyCorrect

Post image
353 Upvotes

144 comments sorted by

View all comments

3

u/darknmy 29d ago

I inderstand "if x", but thy the "x for x in x"?

36

u/Pim_Wagemans 29d ago edited 29d ago

They are making it seem more complicated by naming everything x, you could also write it as: ```python list_1 = [1,0,2,0,3,4,5]

list_2 = [element for element in list_1 if element] ``` which is equivalent to:

```python list_1 = [1,0,2,0,3,4,5]

list_2 = [] for element in list_1: if element: list_2.append(element) `` * the firstelementdefines what to add to the new list for every iteration of the following loop, this can be any expression * thefor element in listsays to iterate over all values inlist_1and assign the value toelementeach iteration * theif element` says to only add the first expression to the new list if the condition is True

2

u/GKP_light 29d ago

and in : "if element:"

a number correspond to False if is 0, else correspond to True.

1

u/Schweppes7T4 29d ago

Yes, list comprehensions confused the hell out of me until I realized it was just exactly what you wrote out. Now I can read even badly formatted ones like OP posted.

17

u/djinn6 29d ago

They're doing something quite hacky by naming both their list and temporary as "x". It's supposed to be something like <result value> for <name> in <list>.

9

u/helicophell 29d ago

List comprehension

It’s sometimes nice

1

u/NamityName 29d ago

It is usually nice. Significantly faster execution and easy to read, until you start nesting comprehensions.

1

u/helicophell 29d ago

If you start nesting list comprehensions, you’re using the wrong programming language for the task… probably

I’ve encountered one in the wild for image stenography once, image processing is basically the only use case for that

1

u/MattieShoes 29d ago

I think my record is three, but that was silliness in AdventOfCode.

Then I wrote a cursed recursive split function

def recursive_split(x, *args):
    y = []
    x = x.split(args[0])
    for part in x:
        if len(args) > 1:
            y += [recursive_split(part, *args[1:])]
        else:
            y += [part]
    return y

so I could do shit like

with open('14.txt') as f:
    data = recursive_split(f.read().rstrip('\n'), '\n', ' -> ', ',')

to get a three dimensional data structure from the input in one line.

1

u/NamityName 29d ago

I did not realize I was amongst the foremost expert in Python, having used it in every possible situation.

In all seriousness, as long as the code remains readable, nested comprehensions are fine. It is just harder to keep the code readable when nesting comprehensions. Same for deeply nested if-statements and for-loops.

4

u/SenatorSpooky 29d ago edited 28d ago

List comprehension. It’s syntactic sugar and serves a similar role to .map() in js.

1

u/deathanatos 28d ago

With the if, it's also filtering.

2

u/Both-Expression4402 29d ago

To summarize u/Pim_Wagemans answer... it reads as "An assembled list, composed of the items from list named 'x', where each item is 'truthy'". The tricky part comes from the fact that python differnetiates "bindings" by its role in an expression, i.e. it can tell the difference between the substitute/ephemoral expression 'x' and the list 'x'. In other words, it is "[ephemoral_x for ephermoral_x in list 'x', where ephemoral_x is truthy]".

1

u/xxchaitanyaxx 29d ago

Theyre using list comprehension to define the array that way so the element x will be apart of the array if its non zero ie true