YouTip LogoYouTip

Python3 Func Enumerate

# Python3.x Python3 enumerate() Function [![Image 3: Python3 Built-in Functions](#) Python3 Built-in Functions](#) * * * `enumerate()` is a built-in Python function used to obtain both the index and value while iterating over an iterable object. Using `enumerate()` allows you to conveniently get both the index (position) and the element itself, avoiding the need for an extra counter variable. **Word Definition**: `enumerate` means "to list one by one." * * * ## Basic Syntax and Parameters ### Syntax Format enumerate(iterable, start=0) ### Parameter Description * **Parameter iterable**: * Type: Iterable object * Description: The iterable object to be enumerated. * **Parameter start** (optional): * Type: Integer * Description: The starting value of the index, defaults to 0. ### Function Description * **Return Value**: Returns an enumerate object (iterator). * **Output**: Each iteration returns an (index, value) tuple. * * * ## Examples ### Example 1: Basic Usage ## Example # Basic enumerate usage fruits =["Apple","Banana","Orange"] # Get index and value for index, fruit in enumerate(fruits): print(f"{index}: {fruit}") # Default starts from 0 print("---") for index, fruit in enumerate(fruits): print(fruit) **Expected Output:** 0: Apple1: Banana2: Orange---0: Apple1: Banana2: Orange **Code Explanation:** 1. enumerate returns (index, value) tuples. 2. The default index starts from 0. ### Example 2: Specifying a Starting Index ## Example # Numbering starting from 1 fruits =["Apple","Banana","Orange"] for i, fruit in enumerate(fruits, start=1): print(f"Fruit #{i}: {fruit}") # Starting from 10 print("---") for i, fruit in enumerate(fruits, start=10): print(f"Index {i}: {fruit}") **Expected Output:** Fruit #1: AppleFruit #2: BananaFruit #3: Orange---Index 10: AppleIndex 11: BananaIndex 12: Orange The start parameter can specify the starting index. ### Example 3: Converting to a List ## Example # Convert to a list fruits =["Apple","Banana","Orange"] result =list(enumerate(fruits)) print(result)# Output: [(0, 'Apple'), (1, 'Banana'), (2, 'Orange')] # Convert to a dictionary (if values are strings) result =dict(enumerate(fruits)) print(result)# Output: {0: 'Apple', 1: 'Banana', 2: 'Orange'} # Used with list comprehension squares =[x**2 for x in range(5)] indexed_squares =[(i, v)for i, v in enumerate(squares)] print(indexed_squares)# Output: [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)] **Expected Output:** enumerate can be conveniently converted to a list or dictionary. ### Example 4: Practical Applications ## Example # Find the index of an element fruits =["Apple","Banana","Orange","Banana","Apple"] target ="Banana" for index, fruit in enumerate(fruits): if fruit == target: print(f"Found {target} at index {index}") break # Find all matching items with a condition print("nAll positions of Banana:") for index, fruit in enumerate(fruits): if fruit =="Banana": print(index, end=" ")# Output: 1 3 # Find character positions in a string text ="hello" for i, c in enumerate(text): print(f"'{c}' is at position {i}") **Expected Output:** enumerate is commonly used in scenarios requiring an index, such as search, replace, and other operations. ### Example
← Python3 Func BytesPython3 Built In Functions β†’