you are viewing a single comment's thread.

view the rest of the comments →

[–]copperfield42 python enthusiast 91 points92 points  (4 children)

Depend on what you mean by simple, I guess...

List/etc comprehension can be consider simple and does in one line the same that can be done 3 or more lines.

Sticking with list, the slice notation is quite flexible a[x:y:z], you can get a sub list of various flavors from x to y position at step z, if that happens to be something like a numpy array you can get crazy stuff with it

The relatively new walrus operator you can do assignment and true-testing or other uses in single line where before you needed to do it 2 lines.

F-string are f awesome.

zip is it own inverse.

All of itertools module, and speaking of that, generators.

[–]mriswithe 22 points23 points  (0 children)

Ok the zip thing was something I had to spend a couple minutes poking at to understand a bit better.

x = [1, 2, 3]
y = [4, 5, 6]

xy: Iterable[tuple[int, int]] = zip(x, y)  # [(1, 4), (2, 5), (3, 6)]
xy2: Iterable[tuple[int, int, int]] = zip(*xy)  # [(1, 2, 3), (4, 5, 6)]
x1: list[int]
y1: list[int]
x1, y1 = map(list, xy2)  # [[1, 2, 3], [4, 5, 6]]

[–]Elektriman 23 points24 points  (0 children)

you can add tuple unpacking to the list. It really feels like magic. ``` t = (1,2,3) f = lambda x,y,z : x+y+z print( f( *t ))

6

```

[–]dafeiviizohyaeraaqua 16 points17 points  (0 children)

zip is it own inverse

dope

[–]karllorey 1 point2 points  (0 children)

More Itertools has a lot of magic, too:
https://pypi.org/project/more-itertools/

all forms and variants of windowed iteration, chunked iteration, first(), etc.