What is enumerate() function in python
The enumerate() function returns the length of an iterable and loops through its items simultaneously. Thus, while printing each item in an iterable data type, it simultaneously outputs its index.
Assume that you want a user to see the list of items available in your database. You can pass them into a list and use the enumerate() function to return this as a numbered list.
Here's how you can achieve this using the enumerate() method:
Whereas, you might've wasted valuable time using the following method to achieve this:
In addition to being faster, enumerating the list lets you customize how your numbered items come through.
In essence, you can decide to start numbering from one instead of zero, by including a start parameter:
Output:
The enumerate() function returns the length of an iterable and loops through its items simultaneously. Thus, while printing each item in an iterable data type, it simultaneously outputs its index.
Assume that you want a user to see the list of items available in your database. You can pass them into a list and use the enumerate() function to return this as a numbered list.
Here's how you can achieve this using the enumerate() method:
fruits = ["grape", "apple", "mango"]
for i, j in enumerate(fruits):
print(i, j)
Output:
0 grape
1 apple
2 mango
Whereas, you might've wasted valuable time using the following method to achieve this:
fruits = ["grape", "apple", "mango"]
for i in range(len(fruits)):
print(i, fruits[i])
In addition to being faster, enumerating the list lets you customize how your numbered items come through.
In essence, you can decide to start numbering from one instead of zero, by including a start parameter:
for i, j in enumerate(fruits, start=1):
print(i, j)
Output:
1 grape
2 apple
3 mango
👍16
QUANTUM_COMPUTING_WITH_PYTHON_The_new_comprehensive_guide_t.epub
23.3 MB
Quantum Computing with Python
Jason Test, 2021
Jason Test, 2021
👍4🔥2
kevin-wilson-the-absolute-beginner-s-guide-to-python.pdf
13.2 MB
The Absolute Beginner's Guide to Python Programming
Kevin Wilson, 2022
Kevin Wilson, 2022
Learning_Ray_Flexible_Distributed_Python_for_Machine.pdf
4.2 MB
Learning Ray
Max Pumperla, 2023
Max Pumperla, 2023
👍3
PythonProgrammingExercisesGentlyExplained.pdf
1.5 MB
Python Programming Exercises, 2022., gently explained
42 programming exercises on 160 pages with plain-English explanations
Exercise #1: Hello, World!
Exercise #2: Temperature Conversio
Exercise #3: Odd & Even
Exercise #4: Area & Volume
Exercise #5: Fizz Buzz
Exercise #6: Ordinal Suffix
Exercise #7: ASCII Table
Exercise #8: Read Write File
Exercise #9: Chess Square Color
Exercise #10: Find and Replace
Exercise #11: Hours, Minutes, Seconds
Exercise #12: Smallest & Biggest
Exercise #13: Sum & Product
Exercise #14: Average
Exercise #15: Median
Exercise #16: Mode
Exercise #17: Dice Roll
Exercise #18: Buy 8 Get 1 Free
Exercise #19: Password Generator
Exercise #20: Leap Year
And many more
42 programming exercises on 160 pages with plain-English explanations
Exercise #1: Hello, World!
Exercise #2: Temperature Conversio
Exercise #3: Odd & Even
Exercise #4: Area & Volume
Exercise #5: Fizz Buzz
Exercise #6: Ordinal Suffix
Exercise #7: ASCII Table
Exercise #8: Read Write File
Exercise #9: Chess Square Color
Exercise #10: Find and Replace
Exercise #11: Hours, Minutes, Seconds
Exercise #12: Smallest & Biggest
Exercise #13: Sum & Product
Exercise #14: Average
Exercise #15: Median
Exercise #16: Mode
Exercise #17: Dice Roll
Exercise #18: Buy 8 Get 1 Free
Exercise #19: Password Generator
Exercise #20: Leap Year
And many more
👍13🔥13👎1😁1
Python 3 - The Comprehensive Guide to Hands-On Python.pdf
17.1 MB
Python 3
Johannes Ernesti, 2022
Johannes Ernesti, 2022
👍7❤1