- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 convert a list of strings with a delimiter to a list of tuple
When it is required to convert a list of strings with a delimiter to a list of tuple, a list comprehension, the ‘tuple’ method, and the ‘split’ method are used.
Below is a demonstration of the same −
my_list = ["21$12", "33$24$48$69", "14$10$44"] print("The list is :") print(my_list) key = "$" print("The key is :") print(key) my_result = [tuple(int(element) for element in sub.split(key)) for sub in my_list] print("The result is :") print(my_result)
The list is : ['21$12', '33$24$48$69', '14$10$44'] The key is : $ The result is : [(21, 12), (33, 24, 48, 69), (14, 10, 44)]
A list of string values is defined and is displayed on the console.
A key value is defined and displayed on the console.
A list comprehension is used to iterate over the list.
It is split based on the ‘key’ that was previously defined.
It is then converted into an integer and then to a list of tuple.
This is assigned to a variable.
This is displayed as output on the console.
- Python – To convert a list of strings with a delimiter to a list of tuple
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Convert a list into tuple of lists in Python
- Convert a list into a tuple in Python.
- How to convert a list into a tuple in Python?
- How to create a tuple from a string and a list of strings in Python?
- Python program to convert a list into a list of lists using a step value
- Python program to convert a list of characters into a string
- Python How to sort a list of strings
- Convert case of elements in a list of strings in Python
- Convert list of strings and characters to list of characters in Python
- Python program to convert a list of tuples into Dictionary
- Python program to convert a list to string
- Python Program to Convert a given Singly Linked List to a Circular List