r/PythonLearning • • 5d ago

i am stuck with the range function

sorry but i am gonna crash out because of the range function i cant understand it well to use it this is driving me crazy any one could give me a video explaining everything i dont mind it being an hour long

9 Upvotes

33 comments sorted by

View all comments

Show parent comment

1

u/Dry-Aioli-6138 1d ago

Well, that's one way to do it. Not very pythonic imho. I would just make pairs of number from the list and a copy of that list, but with numbers shifted by one (throwing out the first one)

ni = [2,4,6,9,12]
pairs = []
for pair in zip(ni, ni[1:]):
# zip function combines nth expressions of all given collections. Very useful
  print(sum(pair))

You can do it in a one liner

ni=[2,4,6,9,12]
print([a+b for a, b in zip(ni, ni[1:])])