Enumerate - Intermediate Python Programming p.7
Skills:
Python for Data80%
Key Takeaways
The video covers the built-in Python function enumerate, which returns a tuple containing a count and the object being iterated over, and demonstrates its use in various scenarios, including iterating over lists and dictionaries.
Full Transcript
what is going on everybody Welcome to part seven of our intermediate Python Programming tutorial Series in this tutorial we're going to be talking about the built-in function enumerate so what enumerate is going to do is it's going to return a tuple containing the basically the count along the way and then the object itself or the item itself that you were iterating over so you use enumerate on iterable so where's my mouse first let's say uh we're just going to say an example equals uh left right up and down and what we're going to say is we'll just do uh we'll do 4 I in range Len example print I example I so this is an example of code that you might find yourself writing to produce the following like basically anytime you find yourself or I would say most of the time I'm trying to think of an example where I've not been screwing up when I do this but pretty much anytime you're going to say 4 I in range of a length of something you're probably doing it wrong okay so just keep that in mind I've done it probably no I know I've done it in some of the tutorials for sure um just because like I just never use enumerate I just don't think about it but lately in my own code I've been trying to use enumerate just because it exists and you should do it so anyways this is not the right way to go about it instead what you should say is something like this for i j in enumerate example print I um I'll print the other one just to do it I guess but as you can see this one ended here or this is the first one this is the second one exact same output just a little cleaner um it makes a little bit more sense it's the way it's supposed to be you're also not doing like this range Len nonsense you just don't need to uh so don't do it so that's one example of enumerate for most people that's probably all you need to do uh you can use enumerate over a dictionary and a lot of other aables I would show you a dictionary you can go to the sample code if you really want to see it but it's basically the exact same thing as we just did here and I just don't want to make a dictionary but I will also show that you can easily because we have Tuple what can you do with basically list of tupal you can do the following you can say new dictionary we're going to build a dictionary in equals dict enumerate um and then we'll enumerate the example and now let's print new dict there we go so basically in this case we've created a dictionary where the key is that index value of En numerate and then the value is the thing that was in the list now of course since I a second ago I just told you I didn't want to make a dictionary well oh look at that we got a dictionary so we could just do this we'll do list comprehension print i j k or JK that's fine for um let's see print i j k for i j in enumerate new di so here we just get 0 0 1 1 22 3 3 because that is our dictionary was the keys were just 0 1 2 3 and and so on so that was just not you're probably never going to enumerate over a dictionary that uh has keys like that um but just showing that you can do it over a dictionary if you so chose so that's all on enumerate it's just a really simple um addition to the python standard Library something useful mostly for when you find yourself in this case this is just always when you would use enumerate for the most part I'm sure there's lots of other times you might use it are things that you in tasks that you might do but for the most part this is when I found myself doing what I should have been doing enumerate so uh in the next tutorial we're going to be talking about the zip function but if you do have any questions comments concerns on enumerate feel free to leave below otherwise I'll see you in the next tutorial
Original Description
Welcome to part 7 of the intermediate Python programming tutorial series. In this part, we're going to talk about the built-in function: enumerate.
A somewhat common task might be to attempt to attach an index value, or some other unique value to list items. It may be temping to use something a for loop over range(len(your_variable)), but this is usually not quite the right way.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI