DEV Community

Discussion on: What are your favorite programming language syntax features?

Collapse
 
alchermd profile image
John Alcher

List Comprehensions in Python is an interesting one. I absolutely love them in some cases, and despise them on others.

# Simple and elegant...
squares = [n**2 for n in numbers]


list_a = [...]
list_b = [...]
# ...not on this one IMO, I'd prefer loops instead.
different_num = [(a, b) for a in list_a for b in list_b if a != b]
Collapse
 
geocine profile image
Aivan Monceller

It's my first time to see such syntax since I haven't really touched Python. Looks interesting , I couldn't understand what the last line does though.

Collapse
 
defman profile image
Sergey Kislyakov • Edited

The last line is:

for a in list_a:
    for b in list_b:
        if a != b:
            different_num.append((a, b))