
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
Convert List of Tuples to List of Strings in Python
During data processing using python, we may come across a list whose elements are tuples. And then we may further need to convert the tuples into a list of strings.
With join
The join() returns a string in which the elements of sequence have been joined by str separator. We will supply the list elements as parameter to this function and put the result into a list.
Example
listA = [('M','o','n'), ('d','a','y'), ('7', 'pm')] # Given list print("Given list : \n", listA) res = [''.join(i) for i in listA] # Result print("Final list: \n",res)
Output
Running the above code gives us the following result −
Given list : [('M', 'o', 'n'), ('d', 'a', 'y'), ('7', 'pm')] Final list: ['Mon', 'day', '7pm']
With map and join
We will take a similar approach as above but use the map function to apply the join method. Finally wrap the result inside a list using the list method.
Example
listA = [('M','o','n'), ('d','a','y'), ('7', 'pm')] # Given list print("Given list : \n", listA) res = list(map(''.join, listA)) # Result print("Final list: \n",res)
Output
Running the above code gives us the following result −
Given list : [('M', 'o', 'n'), ('d', 'a', 'y'), ('7', 'pm')] Final list: ['Mon', 'day', '7pm']
Advertisements