
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to iterate through a tuple in Python?
There are different ways to iterate through a tuple object. The for statement in Python has a variant which traverses a tuple till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is −
for var in tuple: stmt1 stmt2
Example
Following script will print all items in the list
T = (10,20,30,40,50) for var in T: print (T.index(var),var)
Output
The output generated is −
0 10 1 20 2 30 3 40 4 50
Another approach is to iterate over range upto length of tuple, and use it as index of item in tuple
Example
for var in range(len(T)): print (var,T[var])
You can also obtain enumerate object from the tuple and iterate through it.
Output
Following code too gives same output.
for var in enumerate(T): print (var)
- Related Questions & Answers
- Iterate through Java Unit Tuple
- Iterate through Java Pair Tuple
- Iterate through Decade Tuple in Java
- Iterate through LabelValue Tuple in Java
- Iterate through Ennead Tuple in Java
- Iterate through Octet Tuple in Java
- Iterate through KeyValue Tuple in Java
- How to iterate through a dictionary in Python?
- How to iterate through a list in Python?
- How to iterate over a C# tuple?
- How we can iterate through a Python list of tuples?
- How to correctly iterate through getElementsByClassName() in JavaScript?
- Iterate through ArrayList in Java
- Python - Ways to iterate tuple list of lists
- How can I iterate through two lists in parallel in Python?
Advertisements