Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic.
The better option is to use the built-in function enumerate()
, available in both Python 2 and 3:
for idx, val in enumerate(ints): print(idx, val)
Related Posts:
- Python – TypeError: ‘int’ object is not iterable
- Python – TypeError: ‘int’ object is not iterable
- Difference between del, remove, and pop on lists
- How to overcome TypeError: unhashable type: ‘list’
- Accessing the index in ‘for’ loops?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How can I reverse a list in Python?
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Understanding slice notation
- How to update/upgrade a package using pip?
- How do I update\upgrade pip itself from inside my virtual environment?
- IndexError: too many indices for array
- numpy array: IndexError: too many indices for array
- How do I specify new lines on Python, when writing on files?
- bash: pip: command not found
- How do I check what version of Python is running my script?
- How to convert list to string [duplicate]
- Converting integer to string in Python
- Reverse a string in Python
- Python for-in loop preceded by a variable
- What does if __name__ == “__main__”: do?
- How to measure elapsed time in Python?
- How to uninstall Anaconda completely from macOS
- Python: Start and stop timer [duplicate]
- not all arguments converted during string formatting.. NO % variables
- How do I install opencv using pip?
- TypeError: ‘builtin_function_or_method’ object is not subscriptable
- How do I compare two strings in python?
- Curve curvature in numpy
- ImportError: No module named sklearn.cross_validation
- How can I prevent the TypeError: list indices must be integers, not tuple when copying a python list to a numpy array?
- Finding the index of an item in a list
- What are data classes and how are they different from common classes?
- Tensorflow import error: No module named ‘tensorflow’
- Python random function
- Converting NumPy array into Python List structure?
- Converting string into datetime
- ImportError: No module named matplotlib.pyplot
- How do you get the logical xor of two variables in Python?
- ValueError: could not convert string to float: id
- How do I list all files of a directory?
- Pip freeze vs. pip list
- The difference between comparison to np.nan and isnull()
- Best way to return multiple values from a function? [closed]
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- TypeError: ‘float’ object is not subscriptable
- Is there a short contains function for lists?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- Check if something is (not) in a list in Python
- TypeError: ‘list’ object cannot be interpreted as an integer
- Finding median of list in Python
- Finding and replacing elements in a list
- ValueError: max() arg is an empty sequence
- Append integer to beginning of list in Python
- List comprehension on a nested list?
- Union of two lists in Python
- Writing a list to a file with Python
- Subplot for seaborn boxplot
- Understanding slice notation
- Add list to set?
- What’s the idiomatic syntax for prepending to a short python list?
- Converting Dictionary to List?
- How to deep copy a list?
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- Exit while loop in Python
- How do I check if a list is empty?
- How can I get list of values from dict?
- IndexError: list index out of range and python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- Pop index out of range
- When to use “while” or “for” in Python
- Print list without brackets in a single row
- How to iterate through two lists in parallel?
- How to remove \n from a list element?
- How to return dictionary keys as a list in Python?
- Append a tuple to a list – what’s the difference between two ways?
- Defining lists as global variables in Python
- Subtracting two lists in Python
- Breaking out of nested loops
- Python 2: AttributeError: ‘list’ object has no attribute ‘strip’
- How to make a set of lists
- Infinite for loops possible in Python?
- urllib2.HTTPError: HTTP Error 403: Forbidden
- Python/Django: log to console under runserver, log to file under Apache
- TypeError: unsupported format string passed to list.__format__
- numpy-equivalent of list.pop?
- TensorFlow not found using pip
- convert csv file to list of dictionaries
- Element-wise addition of 2 lists?
- How to split elements of a list?
- Find object in list that has attribute equal to some value (that meets any condition)
- Iterating through list of list in Python
- Python cant convert ‘list’ object to str error [closed]
- Get difference between two lists
- Determine if 2 lists have the same elements, regardless of order? [duplicate]
- Building a list inside a list in python