6 Tips to write BETTER For Loops in Python

6 Tips to write BETTER For Loops in Python

Patrick Loeber

1 год назад

247,503 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Christopher
Christopher - 24.09.2023 17:58

for the iterator comprehension, you can get rod of those ugly indices with
study_times = (minutes for task, minutes in events if task == "learn")

You also won't make the error to use it twice if you put it directly in the sum call (makes more concise code, too):
minutes_studies = sum(minutes for task, minutes in events if task == "learn")

Ответить
IM Dash
IM Dash - 24.09.2023 08:36

😢

Ответить
Shuf Lee
Shuf Lee - 18.08.2023 04:41

Stick to the basics. Don't memorize useless stuffs.

Ответить
El e
El e - 13.06.2023 01:29

Okay what is the vs code theme? Oh and good video, really helpful

Ответить
Maxim _
Maxim _ - 29.04.2023 16:56

isn’t an out of range array index supposed to return undefined?

Ответить
Miguel Vásquez
Miguel Vásquez - 14.02.2023 19:03

very interesting the itertools packages, i didnt know about it.

Ответить
Dmitri Pogosian
Dmitri Pogosian - 12.02.2023 05:16

It would be good to mention how much memory each method uses. I have seen some horrific python codes in memory requirements, compounded by the author having no idea why it uses as much memory as it does

Ответить
Anton Pieper
Anton Pieper - 10.02.2023 18:55

7th tip: just use another language like C or Java

Ответить
Daniel Barac
Daniel Barac - 10.02.2023 10:06

Why use islice(lines, 5) instead of lines[:5] ?

Ответить
ZobDeMouche007
ZobDeMouche007 - 05.02.2023 13:42

Great video. Very useful. Thanks

Ответить
The Annoying One
The Annoying One - 28.01.2023 18:22

Syntax error on line 1 in thumbnail 🤓
Also nice vid

Ответить
Listen2LeeK
Listen2LeeK - 17.01.2023 02:59

I learned a lot from this vid; learned even more from the comments 🐍 💬

Ответить
Jared King
Jared King - 15.01.2023 06:10

Whatever man, I'm sticking with my 6 nested for loops. 🤌

Ответить
Jan Kučera
Jan Kučera - 15.01.2023 05:56

clickbait

Ответить
Jay Kay
Jay Kay - 15.01.2023 01:06

No idea about python but in every instance where a for in loop can be used it’s a lot more efficient to use a traditional for loop

Ответить
-ルナール-
-ルナール- - 14.01.2023 10:50

The thumbnail is missing a bracket


You meant to do this instead:

for i in range(len(data)):
print(data[i])

Ответить
sean h
sean h - 13.01.2023 13:05

step 1: ask chatGPT
step 2: copy
step 3: paste

Ответить
DK1PL
DK1PL - 10.01.2023 17:02

What about recursion? p.s. It couldn’t be true that imperative language can't deal with loops or ? 🤔

Ответить
i made eko satria wiguna
i made eko satria wiguna - 07.01.2023 21:27

ah this video too late.. my final project already done 🥲

Ответить
보경이
보경이 - 31.12.2022 18:10

Cool😊

Ответить
Никита Аюханов
Никита Аюханов - 30.12.2022 20:51

What for should i use islice instead of [ : 5]?

Ответить
Andrey Petrov
Andrey Petrov - 30.12.2022 20:23

Bad advise: "write more complex code, and another programmers will spend more and more time to understand your code".

Ответить
XCanG
XCanG - 29.12.2022 04:09

Noting new, but for generator and pairwise I would unpack in for like:

minutes_studied = (minutes for event, minutes in events if event == "learn")

for pair0, pair1 in pairwise(...):

Ответить
Benedikt Maier
Benedikt Maier - 28.12.2022 14:54

Anything is better than your thumbnail code because that doesn’t even run

Ответить
Francesco Anastasio
Francesco Anastasio - 28.12.2022 01:00

“This is very error-prone”: why?

Ответить
John Sahr
John Sahr - 26.12.2022 00:23

You know, I get all these "use native methods" things in Python, but at what point does Python become less readable because there are all these little native functions?

Wouldn't it be better to let an optimizer turn a for loop into a list comprehension? Just how, exactly, is a list comprehension always better than a for loop in terms of readability? Sure, it's better for speed, but is it better for readability?

It would be an error if Python got turned into APL (a fabulous, but write-only language).

-----

Fire is a good servant, but a bad master.

Let us have computer languages which serve us, rather than having us serve them.

Ответить
John Allsup
John Allsup - 25.12.2022 03:23

Something I dealt with recently is where I wanted to loop through the lines of a text file, find the first occurrence of e.g. "hello", and then loop from that point onwards, to find the first occurrence of "random", and, only if we find "random", go back through the lines between that containing "hello" and that containing "random". Iterators make it hard to consume, and harder still to backtrack, things that are easy with C's for(i=1;i<10;i++) construct.

Another is where if an element of a sequence matches some pattern, you want to consume and process the next element of the sequence before the loop continues. (The latter is a case where you have to do for x in I := iter(something): ... y= next(I)

and so on.

It would be nice to see a video where you show genuine cases where you have to use the 'ugly' loop construct, and where things like sum and zip aren't any help.

Ответить
Joshua Worman
Joshua Worman - 23.12.2022 21:55

i hate to be that guy. but it is impressive how bad python messed up how loops are implemented. when in other languages its just a goto. wonder why they did that. thank god for pypy.

Ответить
cmilkau
cmilkau - 23.12.2022 04:30

Now sum() is better style than "for" anyway. But other than that, if these performance differences matter to you, don't use CPython. Use a python interpreter with a good JIT that can optimise away these differences rather than writing fine-tuned code for a slow interpreter.

Ответить
Michael00000001
Michael00000001 - 22.12.2022 13:53

Great video. Also for an old dog good as a reminder. np is a powerful tool (like pandas) but very annoying are their type differences, e.g. None, float, etc.

Ответить
Hardcore Remixer
Hardcore Remixer - 20.12.2022 14:30

Welp, may as well write my loop code in C/C++ and import it in python.

Ответить
Шу Рик
Шу Рик - 18.12.2022 16:48

Is islice() == [::]?

Ответить
Mails
Mails - 15.12.2022 21:51

I'm thinking lazy, and didn't get a point of using "islice" instead of standart "print(line) for line in lines[1:5:2]" slicing.

Ответить
re::liable
re::liable - 13.12.2022 18:48

Pairwise would've been really useful in an AoC problem lol

I really need to read up itertools and functools. There's so much obscure but handy stuff there

Ответить
re::liable
re::liable - 13.12.2022 18:42

Abt the generator tip,

I'd argue it's better to put the generator comprehension inside the function that uses it.

1 reason is as you've shown, storing it in a variable gives the impression that it is reusable when in fact it may be exhausted in some other place.

Another is that I think it's very readable that way, e.g.
sum(
event[1]
for event in events
)

I find that it reads really well with other functions too, namely `any` and `all`, e.g.
all(
event[1] > 10
for event in events
)

Ответить
Szymon Szyszkowski
Szymon Szyszkowski - 11.12.2022 00:19

It is not true, that loops are bad, as they are the place that JIT compilers can shine after all. It is all just about the python implementation

Ответить
Taylor
Taylor - 07.12.2022 02:07

Thanks for the tips, but tell us, what's your VS Code theme?

Ответить
Корнильцев Константин
Корнильцев Константин - 06.12.2022 11:07

Питон это жуткий ущербный язык.

Ответить
Sons of War
Sons of War - 06.12.2022 02:54

not sure if youve seen, but sum() does weird things with floats. for example

my_list = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
sum(my_list) = 0.9999999

if you use numpy, np.sum(my_list) = 1

Can avoid some headaches this way especially when dealing with predictions of propensity where 0.001 can matter.

Ответить
Muh arief
Muh arief - 05.12.2022 18:37

would be interesting if u include benchmark number also. Performance is also vital role when we do refactoring code, besides readability. For me performance is number one becoze python it self already readable by nature.

Ответить
Allez Venga
Allez Venga - 05.12.2022 16:04

Thanks for your sharing

Ответить
Розт Лик
Розт Лик - 05.12.2022 10:49

You forgot range closing ')' in code in the thumbnail.

Ответить
40 / Gulshan
40 / Gulshan - 05.12.2022 10:38

I also like shades of purple 💜

Ответить
Son Of Troy
Son Of Troy - 04.12.2022 18:39

I’ll just keep it simple and use foreach(array $list as $key => &value); loop in php

Ответить
Gax
Gax - 04.12.2022 16:39

I really don't get why people love python, I have to use it daily at work since our backend is in python, and I have zero fun using it, I feel the language is not helping me, it is in my way

Ответить
world wrestling
world wrestling - 04.12.2022 12:43

Please tell me which plugin did you use in the design

Ответить
__________________________________________________
__________________________________________________ - 04.12.2022 09:32

vs code theme name ?

Ответить
V0W4N
V0W4N - 04.12.2022 01:01

coding is one of the few activities where being lazy is actually helpful

Ответить
vorpal22
vorpal22 - 03.12.2022 23:56

Did not know about the strict parameter added to zip in Py3.10. Nice!

Ответить