- 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 – 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 tuples, a K value is set, and list comprehension along with the ‘split’ method is used.
Below is a demonstration of the same −
my_list = ["33-22", "13-44-81-39", "42-10-42", "36-56-90", "34-77-91"] print("The list is : " ) print(my_list) print("The sorted list is ") my_list.sort() print(my_list) K = "-" print("The value of K is ") print(K) my_result = [tuple(int(element) for element in sub.split(K)) for sub in my_list] print("The resultant list is : ") print(my_result)
The list is : ['33-22', '13-44-81-39', '42-10-42', '36-56-90', '34-77-91'] The sorted list is ['13-44-81-39', '33-22', '34-77-91', '36-56-90', '42-10-42'] The value of K is - The resultant list is : [(13, 44, 81, 39), (33, 22), (34, 77, 91), (36, 56, 90), (42, 10, 42)]
A list of strings is defined and is displayed on the console.
The list is sorted using a sort method and is displayed on the console.
The value for K is defined and displayed in the console.
A list comprehension is used to iterate over the list, and the value is split and converted to an integer and then to a tuple.
This is assigned to a result.
This is displayed as output on the console.
- Python program 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 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 - Convert list of string to list of list
- How to sort a list of strings in Python?
- Python program to convert a list into a list of lists using a step value
- Convert list of string to list of list in Python
- Convert list of tuples to list of list in Python