0

I want to find range of numbers between ith, ith + 1 elements of a list, for example, given a list [1, 3, 5, 7], I want to return range(1,3), range(3,5), range(5, 7). Is there a better way to doing this? See my code below, it works but I want to assume its not efficient because each loop check if its the last item in the list, any better way to do this?

df = pd.read_csv('file.csv')
itemList = df['A'].values.tolist()

for idx, value in enumerate(itemList):
    if itemList[idx]!= itemList[-1]:
        print range(value, itemList[idx + 1])
DougKruger
  • 4,424
  • 14
  • 41
  • 62
  • 5
    `[range(*x) for x in zip(lst, lst[1:])]` – vaultah Aug 17 '16 at 09:17
  • An alternative way to do the unpacking of the `range` args: `[range(start, stop) for start, stop in zip(lst, lst[1:])]`. True, it's not as compact, but it's a little more explicit. :) – PM 2Ring Aug 17 '16 at 09:32

0 Answers0