Skip to playerSkip to main content
🐍 Make your Python loops cleaner with enumerate()!

Instead of creating and updating a separate counter variable, Python's enumerate() function lets you access both the index and the item directly.

Why use enumerate()?

✅ No manual index counter
✅ Cleaner and more readable code
✅ Less code to maintain
✅ More Pythonic
✅ Easier to understand

💡 One small Python technique can make your code much cleaner.

📌 Save this video so you can come back to enumerate() when working with Python lists.

Follow for more practical Python, programming, cybersecurity and ICT tutorials.

#Python #PythonProgramming #LearnPython #PythonTips #CodingTips #ProgrammingTips #LearnToCode #PythonDeveloper #Coding #Programming #SoftwareDevelopment #WebDevelopment #DeveloperTips #TechTips #ComputerProgramming #ICT #DigitalSkills #CodingEducation

Category

📚
Learning
Transcript
00:01Hello, guys. Welcome once again.
00:05In any programming language, there's usually the first part that you look at, and that is called the fundamentals.
00:15Also, in Python programming language, it is one of the easiest languages because the learning curve for Python is a
00:24bit small compared to the one in other languages.
00:31As much as the learning curve is not that steep in Python, there is the hard way of doing things
00:39and there is the easy way of doing things.
00:41The hard way of doing things usually happens when you just know the fundamentals only.
00:49But when you want to discover how easy it can be to implement things in Python programming language, you have
00:56to go deeper.
01:00Apart from just the fundamentals, you have to look at other data structures that are incorporated in Python programming language.
01:09Now, for today, I want to show you an example of how you can write a program to solve a
01:19problem the easy way and the hard way.
01:24Based on whether you know the fundamentals plus the deeper parts of it, functionalities, or just know the fundamentals only.
01:35So for this, I'll be using Visual Studio.
01:40Come to this location, click here.
01:43I want to create a file inside.
01:46I have my folder called tricks.
01:50Inside this folder, I'll create a file called test.py.
01:57Why?
01:59He said, this file, I'll be writing exactly what I'm talking about right now.
02:08So for example, you have a list of items.
02:12You want to look over these items, exposing their index number and also the values to display the two.
02:24So the hard way, this is the hard way you would have to do this.
02:30So let me see, this is the hard way.
02:35The hard way, it is, let's say you have a fruits.
02:43So this is my list of items.
02:45Fruits, mango, banana, apple.
02:55So I have three fruits there.
02:58Now, supposing I want to enumerate or go over this, loop over these items, producing the index and the value
03:11itself.
03:12I want to display both.
03:13How will I go about it?
03:15So the hard way, it is to have an index here.
03:22And then you'll come and say for the item in.
03:28So I'm using membership operator.
03:31In is an example of membership operator.
03:33They're usually two in and not in.
03:35So item in fruits.
03:39Or I can just use the name itself.
03:42Let's say for fruit in fruits.
03:48Then I want to print.
03:51The index.
03:56And the item.
03:59Then I want to do index with the increment.
04:05So this is a compound assignment operator.
04:09They usually, it's usually a combination of arithmetic operators with the assignment operator.
04:17Basically, it is a shorthand operator.
04:20So that is it.
04:22Save.
04:23Next, we need to run this program.
04:25So go to the desktop.
04:30Where do I want to go to?
04:32That is coding.
04:34No.
04:35Wrong spelling.
04:37Clear the screen.
04:38Where do I want to go?
04:40This is Python.
04:43Clear the screen.
04:45I want to go to TRIX folder.
04:50So CD.
04:52TRIX folder.
04:54Here's the screen.
04:56Inside here I have my file called test.
05:01Let me double check.
05:03It's called test.
05:05Test.
05:06So inside there I will come and type Python test dot UI.
05:15Okay.
05:16I'm getting an error.
05:18And this is one of the most common problems people get.
05:23You find that you are using a different variable as opposed to the one you used while you were writing
05:31the first part of the program.
05:33So always be consistent with the variable names you are using.
05:39Let's correct that immediately.
05:42So I used food.
05:46And now I'm writing here item.
05:49I don't know how many people noticed that.
05:52Well you can write a comment.
05:55If you noticed that before I ran the program the first time.
06:01So let's say fruits.
06:03Index.
06:04It's okay.
06:04We have index there.
06:06Fruit.
06:06It's there.
06:07Let me run this program once again.
06:10So you can see.
06:10Mango.
06:12It's at index position.
06:14Zero.
06:15Banana.
06:16One.
06:16Apple.
06:17Two.
06:18Two.
06:19So I have the indexes and I have the values.
06:23So this is the hard way of doing things in Python.
06:28These are the items.
06:30Item.
06:31And the index position.
06:33Item.
06:34Index position.
06:35This is the hard way.
06:35Now.
06:36When you're talking about the easy way.
06:41that you have to do this.
06:41So do that.
06:41So supposing I want them to display in actual figures.
06:47The ones we use.
06:48Not the computer numbering system.
06:51I will use.
06:52I'll add one here.
06:56Now.
06:56When I run this program.
06:57I get 1, 2, 3.
07:00Now.
07:04So, there is the easy way of doing this.
07:13And the easy way, we are using a function called a numerate function.
07:29The easy way uses a numerate function.
07:32So, a numerate allows you to supply two variables when you are doing iteration.
07:40The index and the variable.
07:45The iterator variable.
07:48How? This is how it works.
07:50So, I will say four.
07:52I will say the index position I want to retrieve.
07:55I want also to retrieve the fruit and its index position in a numerate.
08:04Inside here, I will put my list of fruits.
08:11The reason I am saying it is a function is because it has normal brackets.
08:16Then I am supplying an argument.
08:18So, inside here, an argument is the fruit, which is the least variable.
08:24That is the argument I am passing inside this function called a numerate.
08:29So, this way, when I print, I just need to supply the index, position, and a foot.
08:41That is it.
08:42So, you can see the difference in terms of the number of lines I had to write to solve this
08:50problem.
08:53Here, I have seven lines.
08:56Here, I have seven lines.
08:57Here, I have ten lines.
08:59Only two.
09:00From ten to twelve.
09:03So, learning Python fundamentals only, it's not okay.
09:10You have to look deeper into it.
09:12List compressions.
09:14There is another part also.
09:17I also have other parts where when lists and normal data structures fail, like tuples and dictionaries, what else comes
09:26in?
09:27So, there is another area altogether.
09:31This is an example.
09:33Let's run this.
09:34We see if we are getting a similar thing.
09:41Okay, so, let me, can I freeze one part of this?
09:49Let me just freeze this part so that I get the correct output.
10:00I'm using hashtags there.
10:05Okay.
10:33Let me just freeze this part.
10:37Ah, no, no, no, no, no.
10:39I don't know what I've written today.
10:41And this is it.
10:43I had forgotten here.
10:47I'm supposed to supply the iterator variable, not the list itself.
10:54So, these are some of the common things you might encounter.
10:56But, um, it's late.
11:03So, possibility of problems might arise.
11:08So, make sure, double check, always, eh?
11:12So, this is my list.
11:15You can see I have it there.
11:20Let me clear the screen.
11:22Run the program again.
11:23Then, we have zero, one, two.
11:29Okay, so, that is, uh...
11:33That is the list.
11:35So, we have two, six, six, seven, Nets.
11:49You

Recommended