ENUMERATE in Python

The enumerate() function in Python is a handy built-in function that allows you to loop over an iterable (like a list, tuple, or string) while also keeping track of the index of each item in the iterable. It returns an enumerate object, which yields pairs containing the index and the corresponding item from the iterable.

Basic Syntax

  • iterable: The iterable to be enumerated.
  • start (optional): The index value from which the enumeration should start. By default, it’s 0.

Example

tropical_fruits = [‘Mango’, ‘Pineapple’, ‘Banana’, ‘Coconut’, ‘Papaya’, ‘Guava’, ‘Passionfruit’, ‘Dragon Fruit’, ‘Lychee’, ‘Durian’]

Use Cases:

  1. Sort a list inplace

As you can see, the list is sorted in place and returns

2. Create a new Sorted List

The sorted function returns a sorted list

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top