Yes, you can also use the unpacking operator * with enumerate() to unpack the index and value of each element into separate variables.
Here's an example of using enumerate() with unpacking:
fruits = ["apple", "banana", "cherry"]for i, *rest in enumerate(fruits): print(i, rest) |
In this example, we define a list called fruits containing three fruits. We then use the enumerate() function with unpacking to iterate over the elements of the list and unpack the index and value of each element into separate variables. Inside the loop, the variable i takes on the index of each element, and the variable rest is a list containing the remaining elements of the tuple.
Note that we use the * operator before the variable rest to indicate that it should receive all remaining elements of the tuple after the first element (the index).
I hope this helps! Let me know if you have any other questions.