You can zip
the list with itself sans the first element:
a = [5, 7, 11, 4, 5]
for previous, current in zip(a, a[1:]):
print(previous, current)
This works even if your list has no elements or only 1 element (in which case zip
returns an empty iterable and the code in the for
loop never executes). It doesn’t work on generators, only sequences (tuple
, list
, str
, etc).
Related Contents:
- Strange result when removing item from a list while iterating over it
- What is the most “pythonic” way to iterate over a list in chunks?
- Accessing the index in ‘for’ loops
- Modifying list while iterating [duplicate]
- Can’t modify list elements in a loop [duplicate]
- Removing Item From List – during iteration – what’s wrong with this idiom?
- Iterate through pairs of items in a Python list [duplicate]
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Python – TypeError: ‘int’ object is not iterable
- How do I loop through a list by twos? [duplicate]
- How to iterate over a list in chunks
- Why doesn’t assigning to the loop variable modify the original list? How can I assign back to the list in a loop? [duplicate]
- How can I avoid “RuntimeError: dictionary changed size during iteration” error?
- Assigning values to variables in a list using a loop
- How to transform string of space-separated key,value pairs of unique words into a dict
- Why do I get “List index out of range” when trying to add consecutive numbers in a list using “for i in list”? [duplicate]
- Why does a for-loop with pop-method (or del statement) not iterate over all list elements [duplicate]
- Strange result when removing item from a list while iterating over it in Python
- How to iterate through two lists in parallel?
- How to reverse a list?
- Writing a list to a file with Python
- Create nice column output in python
- Comparing two lists using the greater than or less than operator
- Dictionary: Get list of values for list of keys
- sort() and reverse() functions do not work
- How do I make a flat list out of a list of lists?
- In Python, when to use a Dictionary, List or Set?
- What is the difference between i = i + 1 and i += 1 in a ‘for’ loop? [duplicate]
- Python – Initializing Multiple Lists/Line
- Call int() function on every list element?
- Finding index of an item closest to the value in a list that’s not entirely sorted
- Transform “list of tuples” into a flat list or a matrix
- Sorting a dictionary with lists as values, according to an element from the list
- How can I make a dictionary (dict) from separate lists of keys and values?
- python: most elegant way to intersperse a list with an element
- Get difference between two lists with Unique Entries
- How do I sort a list of objects based on an attribute of the objects?
- Why does “example = list(…)” result in “TypeError: ‘list’ object is not callable”? [duplicate]
- Why can’t I use a list as a dict key in python? Exactly what can and cannot be used, and why?
- Is it possible to make a `for` loop without an iterator variable? (How can I make make code loop a set number of times?)
- How to split by comma and strip white spaces in Python?
- How to read an array of integers from single line of input in python3
- Can lists be mutated? [duplicate]
- Sort list of strings by a part of the string
- Why does using from __future__ import print_function breaks Python2-style print? [closed]
- Duplicate each member in a list [duplicate]
- How to get all combination of n binary value? [duplicate]
- Sort a list with a custom order in Python
- Numpy individual element access slower than for lists
- Python: Elegant way to check if at least one regex in list matches a string