6 Tips to write BETTER For Loops in Python

preview_player
Показать описание
6 Tips to write BETTER For Loops in Python.

Further resource:

Get my Free NumPy Handbook:

📓 ML Notebooks available on Patreon:

If you enjoyed this video, please subscribe to the channel:

~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

#Python

Timeline:
00:00 - Introduction
00:16 - Avoid loops
00:54 - enumerate
01:43 - zip
02:57 - Think Lazy
05:19 - Use itertools more - 3 cool functions
08:00 - Use NumPy

----------------------------------------------------------------------------------------------------------
* This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Рекомендации по теме
Комментарии
Автор

On the other side, if you don't know the particular function (that you or someone else used) it will make the code less readable + and if you don't know all features of the functions you will get lost on finding a bug. Loops are not always bad if well designed inside a concept and using good commentary lines for descriptions/explanations.

GooogleGoglee
Автор

Thank you for another awesome video Patrick! I found your channel watching the Snake game video and yesterday I just posted a video of making my own version of the game. Thank you for your tutorials and giving me inspiration. 🙂

studyingasyouwere
Автор

Very useful! I have always been writing inefficient and unreadble codes since I don't know these advanded methods.

fengjeremy
Автор

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
)

re.liable
Автор

Generators are great to save RAM memory for large arrays or Sequences. Although they can be slower than looping on array due to the need of computing each next step. My go to is choose the most readable by default and optimize if you need to.

As for islice, i largely prefer to use the slicing syntax in python which is simple, for exemple my_list[2:5:2] is equivalent of islice(my_list, 2, 5, 2).

Migoyan
Автор

Pairwise & takewhile were new to me. Thanks!!

sanjaysaha
Автор

Useful tips, very good video!
Keep 'em coming my friend!

overwatch
Автор

this is a top tier content as a "cheat sheet" video. Thank you so much, Danke schön

knowledgedose
Автор

These are amazing tips. Thanks so much!

shashankemani
Автор

A gem in a haystack!
Too bad you didn't include the timeit% numbers between the optimized vs unoptimized methods.

That would make the video perfect!

pietraderdetective
Автор

3:38 note that, in the generator expression, you can use tuple unpacking as well.

NateROCKS
Автор

Super cool man, I get more efficient every time you put out a video!

champfisk
Автор

2:25 zip() is also useful with database queries. Say you have a list or tuple of field names: you can use ", ".join(field_names) to format the field names for inclusion in a “select” statement; then call dict(zip()) on the returned tuple and the field names, to create a mapping from field names to field values.

lawrencedoliveiro
Автор

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

miguelvasquez
Автор

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

vorpal
Автор

Cool Tips, thanks for sharing, Patrick :)

GATEPREP
Автор

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.

muharief
Автор

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.

Chalisque
Автор

Strange that you haven't mentioned, but when you want to use array elements as function arguments, you can simply write *array
(most useful example: print(*array) will print all of array elements separated by spaces (use argument sep='\n' to write each value into new line) )

rxxubsc
Автор

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

andreypetrov
welcome to shbcf.ru