
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Flatten Tuples List to String in Python
When it is required to flatten a list of tuples into a string format, the 'str' method and the 'strip' method can be used.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A list of tuples basically contains tuples enclosed in a list. The 'strip' method will remove the specific character/value. The 'str' method will convert the given data type into a string data type.
Below is a demonstration of the same −
Example
my_list = [(11, 14), (54, 56), (98, 0), (13, 76)] print("The list is : ") print(my_list) my_result = str(my_list).strip('[]') print("The list of tuple flattened to string is: ") print(my_result)
Output
The list is : [(11, 14), (54, 56), (98, 0), (13, 76)] The list of tuple flattened to string is: (11, 14), (54, 56), (98, 0), (13, 76)
Explanation
- A list of tuple is defined, and is displayed on the consol.
- The strip method is used to remove the '[' and ']' brackets.
- The above method is used after converting the given list of tuple to a string, which is done using the 'str' method.
- This is assigned to a value.
- It is displayed on the console.
Advertisements