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 list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )] print (list_of_tuples) |
[(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 list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )] # Print List of Tuples Using for Loop for item in list_of_tuples: print (item) |
(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 list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )] # Using List Comprehension to print the list of tuples [ print (item) for item in list_of_tuples] |
(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
# imprting pprint module import pprint # List of tuples list_of_tuples = [( 1 , 'DSA' ), ( 2 , 'Java' ), ( 3 , 'Python' )] # Printing List of Tuples Using the pprint Module pprint.pprint(list_of_tuples) |
[(1, 'DSA'), (2, 'Java'), (3, 'Python')]
Print a List of Tuples in Python
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.
Contact Us