Skip to playerSkip to main content
  • 7 months ago
🧠 Want to loop through lists and get both the value and its position?
In this beginner-friendly Python tutorial, we’ll explore how to iterate over lists using regular for loops and how to supercharge your loops with the built-in enumerate() function. Understanding these
Concepts are essential for writing clean, efficient, and professional-grade Python code.
https://1stepgrow.com/advanced-data-science-and-ai-course/

Perfect for students, beginners, or anyone looking to master Python list handling with more control and clarity.
Transcript
00:00okay how can we get the index you can use the brute force method so you can say for i in range
00:13starts from zero goes up to length of shopping underscore list now what does this tell us it
00:19tells us that the range starts from index zero goes up to number of shopping list minus one
00:25because the last element is exclusive the first element is included the last element is not
00:31included and if i would print the value of i i would get these values zero one two three four
00:37five six seven eight till the value 11 okay that means i have 12 elements in the list the list
00:44starts at index zero goes up to index 11 now i can say shopping okay i will just show you a couple
00:52of examples i can say shopping underscore list index zero is equal to curd am i right similarly
01:02shopping list index one is equal to
01:07beat so on and so forth right so similarly when i get the index i can use the index to get the value
01:15so i can say shopping underscore list index i so i would get the value zero curd one wheat two
01:24chocolate three rice four milk so on and so forth this is one way of doing it but there is a more
01:30elegant way and that elegant way is using a built-in method called enumerate now when you write the word
01:37enumerate you will see that enumerate becomes green in color that means enumerate is like the type method
01:43or the print method or the len method it's a built-in python method now what does enumerate do
01:49it takes the iterable in this case it takes the shopping list and it gives you an enumerate object
01:56now the enumerate object is something that cannot be seen by our eyes just like the range object cannot
02:01be seen by us right the range object cannot be seen by us but how can we see the range object we can
02:09see the range object when we convert the range object to a list that time we are able to see the
02:14range object we are able to see what all the range object holds and or when we iterate over the range
02:21object then we are able to see the range object am i right print time we are able to see over the range
02:30object similarly in the enumerate we are not able to see the values of the enumerate right how can we
02:37the values of the enumerate by converting it to a list so i can say list of enumerate e-m-u-m-e-r-a-t-e
02:44underscore list so what are we doing we are converting okay oh sorry my bad my bad
02:54i just got distracted for a second okay so now enumerate underscore list is not defined okay
03:01list so what have i done i have converted the enumerate to this and i get a list where every
03:08element not only contains the value but it also contains the index if you just look at the value
03:13and forget about the circular encapsulation if you just look at the value you will see that
03:18you not only get the index but you also get the value of the element it tells us that at index 0
03:25i have the element curd at index 1 i have the value wheat in index 2 i have the value chocolate
03:31in index 3 i have the value rice so on and so forth this is the power of this is the utility
03:37of an enumerate function now enumerate is also an iterable the enumerate thing is also an iterable
03:43so i can iterate over the enumerate so i can say for element in enumerate e-n-u-m-e-r-a-t-e
03:53enumerating what enumerate the shopping underscore list
03:56okay print element what would you get you would get in circular bracket the index and the value now
04:04again please don't try to wrap your head around the circular bracket we have not covered that yet
04:10right but you just look at the values you get the index and you get the element now what is this
04:17circular bracket thing so if i say type of element you will see that element is of type tuple
04:27so just like a list is encapsulated in circular brackets and i have a number of elements inside
04:33the list right a list is encapsulated in circular brackets and i have some elements encapsulated in
04:39the list similarly a tuple is encapsulated in circular brackets and i can have some elements
04:47inside a tuple this is a tuple now what is the main difference between a list and a tuple
04:53so for example if i have a list of numbers like this this is a type list that is for sure we have
05:03like done list in details right now now what do we know about list a list is mutable that means you can
05:08change the elements of the list that means if i create a variable called new underscore numbers
05:15and i assign it to numbers and then i say new underscore numbers index zero is equal to thousand
05:26now if i print the value of new underscore numbers and i print the value of numbers you would see that the
05:36value of index zero has changed from two ninety two to two thousand that means i was able to modify
05:44an element at index zero hence lists are mutable list can be modified
Be the first to comment
Add your comment

Recommended