You can use a range
with a step size of 2:
Python 2
for i in xrange(0,10,2):
print(i)
Python 3
for i in range(0,10,2):
print(i)
Note: Use xrange
in Python 2 instead of range
because it is more efficient as it generates an iterable object, and not the whole list.
More Related Contents:
- How to transform string of space-separated key,value pairs of unique words into a dict
- Why does a for-loop with pop-method (or del statement) not iterate over all list elements [duplicate]
- Converting for loops to while loops in python
- What is the meaning of list[:] in this code? [duplicate]
- Strange result when removing item from a list while iterating over it in Python
- how to can i go over a file twice?
- Why doesn’t this python code run infinite times? [closed]
- How to append new line to the list
- Python Error with iteration, for loop
- Accessing the index in ‘for’ loops
- Strange result when removing item from a list while iterating over it
- Modifying list while iterating [duplicate]
- Removing Item From List – during iteration – what’s wrong with this idiom?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Can’t modify list elements in a loop [duplicate]
- Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply?
- Loop that also accesses previous and next values
- Is it possible to implement a Python for range loop without an iterator variable?
- Python – TypeError: ‘int’ object is not iterable
- How to loop through all but the last item of a list?
- Converting a list of tuples into a dict
- python assign values to list elements in loop
- Iteration over list slices
- Why do I get “List index out of range” when trying to add consecutive numbers in a list using “for i in list”? [duplicate]
- Are infinite for loops possible in Python? [duplicate]
- interleaving 2 lists of unequal lengths [duplicate]
- Iterate through adjacent pairs of items in a Python list [duplicate]
- How can I make sense of the `else` clause of Python loops?
- for or while loop to do something n times [duplicate]
- How to get the previous element when using a for loop? [duplicate]