
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example
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)
Output
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)]
Explanation
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.
- Related Articles
- 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
- How to convert a list into a tuple in Python?
- Convert a list into tuple of lists 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
- Convert list of strings and characters to list of characters in Python
- Python program to convert a list to string
- Python Program to Convert a given Singly Linked List to a Circular List
- Python How to sort a list of strings
- Convert case of elements in a list of strings in Python
- Python program to convert a list of tuples into Dictionary
- Python program to Flatten Nested List to Tuple List
- Python program to Convert a elements in a list of Tuples to Float

Advertisements