-3

How can I reach the k+1 object in this loop? Is there a way in python like in java or c like for(i=0;...)?

for k in data:
 k+1
toyegej962
  • 21
  • 3
  • what type of data structure belongs `data`?, is it a `list` or `dictionary` or `tuple` or anything else? – A l w a y s S u n n y Feb 08 '20 at 15:54
  • 2
    I think you should go over some tutorials before coming here (no offense). You are asking about very basic stuff. You can use the [`range(len(data))`](https://docs.python.org/3/library/functions.html#func-range) construct, or use the built-in [`enumerate`](https://docs.python.org/3/library/functions.html#enumerate) function – Tomerikoo Feb 08 '20 at 15:54
  • Note in the existing answers so far, they all assume that there *is* a next item, that is, that `k` isn't the last item from the iterator. (I don't want to have to comment on each answer individually.) – chepner Feb 08 '20 at 16:02
  • (They also assume that `data` is indexible, which isn't necessarily true for all iterables.) – chepner Feb 08 '20 at 16:04
  • You may now think about accepting an answer or comment one to get details ;) to reward those who spent time for you ;) – azro Feb 24 '20 at 13:56

2 Answers2

0

You need to iterate using indices, not objects like

for i in range(len(data)-1):
    current = data[i]
    next_one = date[i+1]
azro
  • 53,056
  • 7
  • 34
  • 70
0

You can use enumerate.

for idx,val in enumerate(data[:-1]):
    curr=val
    next_item=data[idx+1]
Ch3steR
  • 20,090
  • 4
  • 28
  • 58