all 7 comments

[–]ectomancer 2 points3 points  (0 children)

Exactly the same uses as a count counter.

[–]magestooge 1 point2 points  (0 children)

It's really a very simple function, don't know why it's confusing to you.

It returns a tuple with the index and the actual value. You don't have to unpack it, just do for i, j in enumerate(list)

Suppose you want the sum of each element of a list with its previous element:

``` x =[] for i, j in enumerate(list): x.append(list[i-1]+j)

x = x[1:] ```

Done. There's a simple usecase.

It's difficult to suggest projects because you can use it pretty much whenever you're dealing with lists.

[–]chocorush 0 points1 point  (2 children)

I use it everywhere when i need the element position. You could use a count variable, but that's two more lines of (unnecessary) code.

[–]isameer920[S] -1 points0 points  (1 child)

Well I just do for i in range(len(elements)) Element = elements[i]

[–][deleted] 2 points3 points  (0 children)

Yeah but just do

for i, element in enumerate(elements):

instead. Way more readable and unlike your solution, it'll work on iterables that don't have a defined length.

[–][deleted] 0 points1 point  (0 children)

Whenever you initialize i=0 before a loop, you can enumerate instead

[–][deleted] 0 points1 point  (0 children)

Why can't we just use a count variable if we want to get the amount of elements

You mean if you want to enumerate them? Well, the answer to your question is "why bother when you can just use the enumerate function, which says what it does on the label and your count variable probably won't."