Python enumerate start at 1

I am using Python 2.5, I want an enumeration like so (starting at 1 instead of 0):

[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

I know in Python 2.6 you can do: h = enumerate(range(2000, 2005), 1) to give the above result but in python2.5 you cannot...

Using Python 2.5:

>>> h = enumerate(range(2000, 2005))
>>> [x for x in h]
[(0, 2000), (1, 2001), (2, 2002), (3, 2003), (4, 2004)]

Does anyone know a way to get that desired result in Python 2.5?

Seanny123

7,95312 gold badges63 silver badges117 bronze badges

asked Jul 21, 2010 at 20:37

3

As you already mentioned, this is straightforward to do in Python 2.6 or newer:

enumerate(range(2000, 2005), 1)

Python 2.5 and older do not support the start parameter so instead you could create two range objects and zip them:

r = xrange(2000, 2005)
r2 = xrange(1, len(r) + 1)
h = zip(r2, r)
print h

Result:

[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

If you want to create a generator instead of a list then you can use izip instead.

answered Jul 21, 2010 at 20:41

Mark ByersMark Byers

778k181 gold badges1551 silver badges1440 bronze badges

5

Just to put this here for posterity sake, in 2.6 the "start" parameter was added to enumerate like so:

enumerate(sequence, start=1)

answered Feb 6, 2013 at 18:28

dhacknerdhackner

2,8222 gold badges19 silver badges23 bronze badges

1

Python 3

Official Python documentation: enumerate(iterable, start=0)

You don't need to write your own generator as other answers here suggest. The built-in Python standard library already contains a function that does exactly what you want:

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

The built-in function is equivalent to this:

def enumerate(sequence, start=0):
  n = start
  for elem in sequence:
    yield n, elem
    n += 1

answered Jun 23, 2019 at 11:52

winklerrrwinklerrr

10.3k5 gold badges65 silver badges79 bronze badges

Easy, just define your own function that does what you want:

def enum(seq, start=0):
    for i, x in enumerate(seq):
        yield i+start, x

answered Jul 21, 2010 at 20:44

DuncanDuncan

87.8k10 gold badges116 silver badges155 bronze badges

Simplest way to do in Python 2.5 exactly what you ask about:

import itertools as it

... it.izip(it.count(1), xrange(2000, 2005)) ...

If you want a list, as you appear to, use zip in lieu of it.izip.

(BTW, as a general rule, the best way to make a list out of a generator or any other iterable X is not [x for x in X], but rather list(X)).

answered Jul 21, 2010 at 21:48

Alex MartelliAlex Martelli

821k163 gold badges1200 silver badges1378 bronze badges

from itertools import count, izip

def enumerate(L, n=0):
    return izip( count(n), L)

# if 2.5 has no count
def count(n=0):
    while True:
        yield n
        n+=1

Now h = list(enumerate(xrange(2000, 2005), 1)) works.

answered Jul 21, 2010 at 20:43

Jochen RitzelJochen Ritzel

101k29 gold badges195 silver badges190 bronze badges

0

enumerate is trivial, and so is re-implementing it to accept a start:

def enumerate(iterable, start = 0):
    n = start
    for i in iterable:
        yield n, i
        n += 1

Note that this doesn't break code using enumerate without start argument. Alternatively, this oneliner may be more elegant and possibly faster, but breaks other uses of enumerate:

enumerate = ((index+1, item) for index, item)

The latter was pure nonsense. @Duncan got the wrapper right.

answered Jul 21, 2010 at 20:49

1

>>> list(enumerate(range(1999, 2005)))[1:]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

answered Jul 21, 2010 at 21:06

JABJAB

20.2k6 gold badges68 silver badges79 bronze badges

3

h = [(i + 1, x) for i, x in enumerate(xrange(2000, 2005))]

answered Jul 21, 2010 at 20:39

Chris B.Chris B.

80.4k24 gold badges94 silver badges135 bronze badges

3

Ok, I feel a bit stupid here... what's the reason not to just do it with something like
[(a+1,b) for (a,b) in enumerate(r)] ? If you won't function, no problem either:

>>> r = range(2000, 2005)
>>> [(a+1,b) for (a,b) in enumerate(r)]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

>>> enumerate1 = lambda r:((a+1,b) for (a,b) in enumerate(r)) 

>>> list(enumerate1(range(2000,2005)))   # note - generator just like original enumerate()
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

answered Jul 22, 2010 at 3:55

Python enumerate start at 1

Nas BanovNas Banov

27.7k6 gold badges46 silver badges67 bronze badges

>>> h = enumerate(range(2000, 2005))
>>> [(tup[0]+1, tup[1]) for tup in h]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

Since this is somewhat verbose, I'd recommend writing your own function to generalize it:

def enumerate_at(xs, start):
    return ((tup[0]+start, tup[1]) for tup in enumerate(xs))

answered Jul 21, 2010 at 20:41

Eli CourtwrightEli Courtwright

178k65 gold badges208 silver badges256 bronze badges

1

I don't know how these posts could possibly be made more complicated then the following:

# Just pass the start argument to enumerate ...
for i,word in enumerate(allWords, 1):
    word2idx[word]=i
    idx2word[i]=word

answered May 27, 2019 at 17:00

Python enumerate start at 1

bmcbmc

7971 gold badge11 silver badges18 bronze badges

Not the answer you're looking for? Browse other questions tagged python or ask your own question.

Does Python enumerate start with 0?

So as you know, indices in python start at 0. This means that when you use the enumerate function, the index returned for the first item will be 0. However, in some cases, you want the loop counter to start at a different number.

How do you range enumerate in Python?

Instead of using the range() function, we can instead use the built-in enumerate() function in python. enumerate() allows us to iterate through a sequence but it keeps track of both the index and the element. The enumerate() function takes in an iterable as an argument, such as a list, string, tuple, or dictionary.

What does enumerate () do in Python?

Python enumerate() Function The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object. The enumerate() function adds a counter as the key of the enumerate object.

Is enumerate fast Python?

Enumerate is the Pythonic way, but unpacking tuples is slower than incrementing locals, and so it will probably always be slower (although only marginally so).