4

By default, enumerate() starts counting at 0 but if you give it a second integer argument, it'll start from that number instead but how can we enumerate from a particular value of count and elem. for example, we want to pass count as '2' and elem as 'bar'. both values have to pass to enumerate function.

elements = ('foo', 'bar', 'baz')
for count, elem in enumerate(elements):
    print (count, elem)
jpp
  • 159,742
  • 34
  • 281
  • 339
aryan
  • 379
  • 1
  • 3
  • 6
  • @Georgy, I don't actually think that's a good target. The questions are subtly different, there's no need for `iter` here for an input list / tuple, and `str.join` is not applicable. – jpp Jan 26 '19 at 19:18

2 Answers2

9

You can iterate a slice of your data and start the enumeration at a specific integer:

start_at = 1
elements = ('foo', 'bar', 'baz')
for count, elem in enumerate(elements[start_at:], start_at):
    print (count, elem)

Output:

1 bar
2 baz

Documentation:


Edit:

If you are working with non-sliceable iterators you have to work around it:

start_at = 3   # first one included (0 based)
stop_at = 5    # last one (not included)

elements = ('foo', 'bar', 'baz', 'wuzz', 'fizz', 'fizzbuzz', 'wuzzfizzbuzz', 'foobarfuzzbizzwuzz')

# skip first few
it = iter(elements)
for _ in range(start_at):
    print("skipping: ", next(it))      # dont print, simply: next(it)

# enumerate the ones you want
i = start_at
for _ in range(stop_at-start_at):
    print(i,next(it))                 # you might need a try: except StopIteration:
    i+=1 

to get:

skipping:  foo
skipping:  bar
skipping:  baz
3 wuzz
4 fizz
5 fizzbuzz
Georgy
  • 12,464
  • 7
  • 65
  • 73
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thankyou for you reply, I have a for loop like this`for batch_id, batch_data in tqdm(enumerate(train_iter)):` , I want to iterate over a particular value of batch_id and batch_data. Can you please suggest me some solution for this please? where batch_id and batch_data i am getting from previous saved values. – aryan Jan 25 '19 at 13:31
  • @aryan is the train_iter an iterable? try consuming the first few outside of your loop: `for _ in range(5): next(train_iter)` to skip first 5 ... – Patrick Artner Jan 25 '19 at 13:36
  • It is actually dataloader from pytorch and I am trying to start training iterations from previously saved values. – aryan Jan 25 '19 at 13:40
  • 1
    @aryan see edit - no idea about your dataloader but this works for iterators – Patrick Artner Jan 25 '19 at 13:44
  • Actually in my case, I just want to give a particular value to batch_id and batch_data, so that enumerate will start from there. where batch_id is int and batch_data is a list. – aryan Jan 25 '19 at 14:01
1

the itertools module is where you should look when you want to iterate in a non standard way

in this case use islice

to get the start index, use the .index function of the container(or hardcode it to 1 if you want to iterate from the second position regardless of the contents), the end index is the len of the container

for count, elem in enumerate(itertools.islice(elements, elements.index('bar'), len(elements))
    print(count, elem)
Derte Trdelnik
  • 2,656
  • 1
  • 22
  • 26