I have a list that contains a random number of ints. I would like to iterate over this list, and if a number and the successive number are within one numeric step of one another, I would like to concatenate them into a sublist.
For example:
input = [1,2,4,6,7,8,10,11]
output = [[1,2],[4],[6,7,8],[10,11]]
The input list will always contain positive ints sorted in increasing order. I tried some of the code from here.
initerator = iter(inputList)
outputList = [c + next(initerator, "") for c in initerator]
Although I can concat every two entries in the list, I cannot seem to add a suitable if
in the list comprehension.
Python version = 3.4