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.
Answer from Mark Byers on Stack OverflowAs 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.
Just to put this here for posterity sake, in 2.6 the "start" parameter was added to enumerate like so:
enumerate(sequence, start=1)
Yup, enumerate() has a start argument. I wish I'd known earlier!
how to make enumerate start from 1 while using zip function ?
It only took me my entire Python career to realize 🤦♂️
Other great values: 1, 100, 42 (?).
Help on class enumerate in module builtins: class enumerate(object) | enumerate(iterable, start=0) | ========> start from wherever! | | Return an enumerate object. | | iterable | an object supporting iteration ...
Any other staples I've missed?
Hi so i have these two lists
a = [1.2.3.4] b= [5,6,7,8]
MY code:
for i,(a,b) in enumerate(zip(a,b)): print(i,a,b)
I get following output
(0, 1, 5) (1, 2, 6) (2, 3, 7) (3, 4, 8)
But i want to start it from 1. i did the following but get the error
for i,(a,b) in enumerate(zip(a,b),start=1): print(i,a,b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
Please Help,Thanks