So I went down a rabbit hole with this one. At first I thought that enumerate is worse since in assembly, its a jmp and then iterating pointer and assigning vs just iterating and assigning. Turns out in newer version of Python they have made enumerate faster than reading and assigning!
Other languages actually have a numeric for loop, and not the odd range() thing. Don't get me wrong, python's for is good and works for everything, but this range() thing is weird
@@juliodonofreo I too was really frustrated with python's for loop in the beginning. But there's a way around it. For example writing something like, for i in range(1,10,2): print(i) Is the same as writing, for(int i=1;i
I've been coding python for more decades than I care to admit and if someone used the first two I wouldn't think they're "noobs", it's perfectly fine. Or conversely, that using the enumerate function is anything to show off about. The fact you think it's advanced says more about you than anything else.
Hey Tophat! I can appreciate your reaction. This short is just intended to be funny. I intentionally went over the top with the "noob" stuff just to make it more interesting in hopes that people would learn something new. Sorry if it came across too harsh.
@@robmulla Oh, now I feel mean! I obviously got the wrong end of the stick, apologies. Don't worry in the slightest, enumerate is a nice solid thing to teach learners for sure, very handy. If I'm honest, it's what I use by default.
I'm a noob both in English and python but. Usually builtin functions in python are faster since they use lower level language. Thus when you use enumerate counters are being assigned in C. If you move this assignment into a python script by using variable inside the loop. You make it a tiny bit slower that cans tack for bigger lists. Same goes for len, you have to access index of the variable every time instead just getting it from the builtin function. And if you just need index and not content itself, you can always use throwaway variable "_"
But why is it so much better? Does it help improve Data management? Is it faster? does it have any substantial advantege against just going through an Loop with the iterable Object range?
performance wise there doesn't seem to be much different, main advantages is its more explicit and shorter line wise, but these tend to minor concerns, it also has the advantage for working on iterables without indexes when that case comes up
It’s easier to work with, let’s say you want to access the index or use it to relate to some data inside a list or a dictionary, is easier to access it with enumerate.
yes. You have both the index and the iterated item. You don't have to worry about how to access the item with an index. It might also be faster too. Probably not though.
Really? Youre going to shame people for using the "for i in range"? It is a core concept in Python, and does exactly what it needs to. Can you give a technical reason for *why* you shouldnt use "for i in range"?
There is none. He shows a solution to a different problem when you have a list of objects you want to iterate and do something with. His use case/example has nothing to do with it and is just slower
As a C guy, I much prefer the "for .. in" construct. _Most_ use cases could just use the "for .. in" construct. Is it too much to ask for both the "for ( ; ; ) { }" _and_ the "for .. in" construct in the same language? That's what I'd like.
Enumerate looked so complicated to me at first when I was a total beginner. Now I kinda feel stupid, it's super easy. But feeling stupid is part of being a programmer I guess.
I see you have spiced thing up come 2023. It is fun to watch. Love to see you work hard everyday with all the shorts and streams. I see that the freq of streams have increased. This motivates me to work harder! Thank you GM!!
If your only goal was to take a list, give it indexes, and print it you could just do this: print(list(enumerate(todo_list))) You can lose the loop, it entails unnecessary overhead for loop control. You had unnecessary string manipulation with the call to the .title() which also cost time. The print function you use in the loop introduces overhead because of the underlying system calls it makes. Making multiple prints can be slower than a single print, especially if there's a lot of data to print.
Bro every type of these videos explaining how to do something a "better" way in code gets spammed with comments calling the creator of the video a clown 😭
I'l give you example. I had to hire two new junior devs as our team grew in past month or so. We had over 300 applicants and few of them had coding channels like these. Every single one was presenting themselves like seniors with 15 years of experience and weren't even able to write proper djisktra. You're better watching people like DavesGarage instead of pseudo-helpful channels like this.
My code might not be efficient, looks like a three year old coded it. But you know what it works for what I want with it. And that in the end is what matters
Syntactic sugar is nice, but honestly, a simple loop counter, either manually or by means of the "for"-statement, is verbose and does not add a ton of clutter or overhead. If you actually need the counter and especially if you do maths with is, I feel a dedicated variable serves you much better.
Well... Under the hood it's basically doing the same thing. I suppose the code is more pythonic, but it only really improves readability if you're familiar with enumerate. Still a neat tip.
Learned this pretty early on, quite often though I wouldn’t care about the index, like your first print example, so it would have been cleaner and more straightforward to do it the “noob” way
Don’t be a noob, use a f-string, don’t be a noob don’t code in toy languages, code in c++, don’t be a noob code in C, don’t be a noob code in asm don’t be a noob code in binary
@@Money_2030 It gets the job done, but not very pythonic. You have to get the length of the list to keep track of the index, then index into the array to get the current fruit - which makes the code more verbose and harder to read. This was the reason in the article😅😂
Well that is a built in function that can do that... but it is VERY slow in big lists, and can dramatically slow a program down with no clear sign of what is slowing things down
Me, the Haskell cultist: zip the entire stream of integers to the list, lift that function postcomposed with putStrLn, and then sequence/commutate the Traversable list with the Monad IO.
Funny enough, i almost always want the list index (or dict key) because i'm modifying the stored values in some way or another, and the value originally stored is still accessible using the variable containing the second value yielded by enumerate(): # In-place equivalent of # slist = [s.title() for s in slist] # because doubling memory costs # doesn't scale for huge lists. for i, s in enumerate(slist): slist[i] = s.title() Maybe that's why Go always provides the index/key when ranging over a slice/map (list/dict in Python)?
I'm a noob too because I often forget to use enumerate or am too lazy to change old code. haha. The noob thing is just a joke to keep people's attention.
enumerate() is a generator that yields a 2-tuple containing a counter and a value. >>> pows2 = [2**p for p in range(8)] >>> for i, x in enumerate(pows2): . . . print(i, x) . . . 0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 As you can see, the first item in the tuple generated by enumerate() can function as a simple counter. You can also pass a _start_ keyword argument to start the counter at a value other than 0, but iteration stops after all items in an iterable have been enumerated.
You learn enumerate in your first programming course within the first 2 weeks. You can tell a real noob when they use enumerate for no reason and don't even use the indexes in their loop.
It seems like enumerate will create a new list and duplicate the contents of the original. Then you loop over it and do whatever. You just increased the amount of memory used and made two loops instead of one. In the context of an actual program that probably doesn’t matter but without any given non-functional requirements it’s quite noobish to call this a noob-move. Maybe the list is huuuge and then this is a noob move.
No, enumerate returns an enumerate object which is an Iterator, it does not create a new list nor it requires any significant memory compared to the size of list(basically its almost free operation, memory wise).
I get that this is supposed to be ina fun format.. but the for loop with range funct seemed just as compact and efficient to me. Is there any advantage to the enumerate method I'm missing?? or, does it really look more pro? haha I wouldn't want to come off as a certified noob!
Enumerate is built by the python devs for this specific case. So to not use it would kind of go against the language. But at the same time, sometimes you just gotta do you.