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)
how to make enumerate start from 1 while using zip function ?
for loop - Python -- how to force enumerate to start at 1 -- or workaround? - Stack Overflow
Have I found my first bug? (for loop with enumerate function)
[deleted by user]
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
Since Python 2.6, enumerate() takes an optional start parameter to indicate where to start the enumeration. See the documentation for enumerate.
You are going to hate this answer for how obvious it is, but you could just do:
if index % 3 == 2: add a horizontal spacer
This adds the spacer after the 2nd element (which is actually the third), and every third element after it.
Hiya.
I'm using Python 3.8 64-bit on Win10, coding in Mu, and have been driven up the wall trying to code something simple for Al's Coin Toss challenge at the end of chapter 4.
So I was taking it line at a time, printing results to see where my error was. Here's an example of one of the loops I used to test:
letters = 'abcdefghijklmnopqrstuvwxyz'
experiment = list(letters)
for index, item in enumerate(experiment):
previousItem = experiment[index - 1]
nextItem = experiment[index+1]
print(index)
print(item)
print(previousItem)
print(nextItem)The first two iterations print:
0
a
z
b
1
b
a
c
which is what I would have expected.
However, when I try to start the loop from index 1 like so:
letters = 'abcdefghijklmnopqrstuvwxyz'
experiment = list(letters)
for index, item in enumerate(experiment, start = 1):
previousItem = experiment[index - 1]
nextItem = experiment[index+1]
print(index)
print(item)
print(previousItem)
print(nextItem)The result is:
1
a
a
c
2
b
b
d
So even though index is correctly starting at 1, the item given is still experiment[0], not experiment[1]. I tried this with enumerate(... start=2) but again, item == 'a' (== experiment[0]) not the item at the correct index.
If I replace print(item) with print(experiment[index]) I get the correct result, but why is it that the item in the iteration doesn't match the index? Isn't that the whole point of the enumerate function? or have I missed something?
Looking forward to a response! This problem has been confusing me for days now before I located it!
duck