Print a List of Tuples in Python
Last Updated :
15 Feb, 2024
Lists and tuples are fundamental data structures in Python, providing versatile ways to store and organize data. Combining these two structures, we can create lists of tuples, offering a powerful means of handling related information. In this article, we will explore different approaches to print the List of Tuples in Python.
Print a List of Tuples in Python
Below, are the approaches to print a list of tuples in Python.
- Using print() Function
- Using for Loop
- Using List Comprehension
- Using pprint module
Print a List of Tuples Using the print() method
The below approach code uses the Python print() function to print the list of tuples in Python.
Python3
list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )]
print (list_of_tuples)
|
Output
[(1, 'DSA'), (2, 'Java'), (3, 'Python')]
Print a List of Tuples Using for Loop
The below approach code uses a for loop to iterate through each tuple in the list_of_tuples and prints each tuple on a separate line, showcasing the elements of the list in a readable format.
Python3
list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )]
for item in list_of_tuples:
print (item)
|
Output
(1, 'DSA')
(2, 'Java')
(3, 'Python')
Print a List of Tuples Using List Comprehension
The below approach code uses list comprehension to iterate through each tuple in the list_of_tuples and simultaneously prints each tuple on a separate line, ensuring proper representation of the tuples.
Python3
list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )]
[ print (item) for item in list_of_tuples]
|
Output
(1, 'DSA')
(2, 'Java')
(3, 'Python')
Print a List of Tuples Using pprint module
The below approach code uses the pprint module to print a formatted representation of the list of tuples. The pprint.pprint() function from the module ensures a visually structured output, making it easier to read and understand the content of the list of tuples.
Python3
import pprint
list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )]
pprint.pprint(list_of_tuples)
|
Output
[(1, 'DSA'), (2, 'Java'), (3, 'Python')]
Share your thoughts in the comments
Please Login to comment...