Use enumerate()
:
>>> S = [1,30,20,30,2]
>>> for index, elem in enumerate(S):
print(index, elem)
(0, 1)
(1, 30)
(2, 20)
(3, 30)
(4, 2)
More Related Contents:
- Accessing the index in ‘for’ loops
- 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?
- Modifying list while iterating [duplicate]
- Iterate through pairs of items in a Python list [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]
- Python – TypeError: ‘int’ object is not iterable
- How to iterate over a list in chunks
- How do I loop through a list by twos? [duplicate]
- Assigning values to variables in a list using a loop
- python assign values to list elements in 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]
- Why doesn’t assigning to the loop variable modify the original list? How can I assign back to the list in a loop? [duplicate]
- Iterate through adjacent pairs of items in a Python list [duplicate]
- How can I avoid “RuntimeError: dictionary changed size during iteration” error?
- Loop over 2 lists, repeating the shortest until end of longest [duplicate]
- Count duplicates between 2 lists
- How to find the maximum number in a list using a loop?
- Strange result when removing item from a list while iterating over it in Python
- Wrapping around on a list when list index is out of range
- Tuple or list when using ‘in’ in an ‘if’ clause?
- Implement list-like index access in Python
- How do I write a loop to repeat the code?
- How to convert a set to a list in python?
- Extracting words from a string, removing punctuation and returning a list with separated words
- How do I send list of dictionary as Body parameter together with Files in FastAPI?