- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to covert tuple into list by adding the given string after every element
When it is required to convert a tuple into a list, by the addition of a string after every element, list comprehension is used.
Below is a demonstration of the same −
my_tup = (56, 78, 91, 32, 45, 11, 23) print("The tuple is : ") print(my_tup) K = "Hi" my_result = [elem for sub in my_tup for elem in (sub, K)] print("The tuple after conversion with K is : ") print(my_result)
The tuple is : (56, 78, 91, 32, 45, 11, 23) The tuple after conversion with K is : [56, 'Hi', 78, 'Hi', 91, 'Hi', 32, 'Hi', 45, 'Hi', 11, 'Hi', 23, 'Hi']
A tuple is defined, and is displayed on the console.
The value of K, a string, is defined.
The list comprehension is used to iterate through the tuple elements, and convert it into a list.
This is assigned to a variable.
This is displayed as output on the console.
Kickstart Your Career
Get certified by completing the courseGet Started