Python program to convert a list of strings with a delimiter to a list of tuple
Last Updated :
08 May, 2023
Given a List containing strings with a particular delimiter. The task is to remove the delimiter and convert the string to the list of tuple.
Examples:
Input : test_list = ["1-2", "3-4-8-9"], K = "-"
Output : [(1, 2), (3, 4, 8, 9)]
Explanation : After splitting, 1-2 => (1, 2).
Input : test_list = ["1*2", "3*4*8*9"], K = "*"
Output : [(1, 2), (3, 4, 8, 9)]
Explanation : After splitting, 1*2 => (1, 2).
Method #1 : Using list comprehension + split()
In this, first, each string is split using split() with K as an argument, then this is extended to all the Strings using list comprehension.
Python3
# Python3 code to demonstrate working of
# Convert K delim Strings to Integer Tuple List
# Using list comprehension + split()
# initializing list
test_list = ["1-2", "3-4-8-9", "4-10-4"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = "-"
# conversion using split and list comprehension
# int() is used for conversion
res = [tuple(int(ele) for ele in sub.split(K)) for sub in test_list]
# printing result
print("The converted tuple list : " + str(res))
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using map() + split() + list comprehension
In this, the task of extension of integral extension logic is done using map() and then list comprehension is used to perform the task of construction of the list.
Python3
# Python3 code to demonstrate working of
# Convert K delim Strings to Integer Tuple List
# Using map() + split() + list comprehension
# initializing list
test_list = ["1-2", "3-4-8-9", "4-10-4"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = "-"
# extension logic using map()
# int() is used for conversion
res = [tuple(map(int, sub.split(K))) for sub in test_list]
# printing result
print("The converted tuple list : " + str(res))
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
The time complexity is O(n*m), where n is the number of elements in the list and m is the maximum length of a single element in the list.
The Auxiliary space is O(nm)
Method #3: Using for loop and append() method
Iterate through each string of the list using a for loop, split each string by the K delimiter, convert the split strings to integers using the map() function, and append the tuple of integers to a result list.
Step-by-step approach:
- Initialize an empty list called 'result' to store the converted tuples.
- Use a for loop to iterate through each string in the input list, 'test_list'.
- Inside the for loop, split each string by the K delimiter using the split() method and store the split strings in a list called 'split_list'.
- Use the map() function with int() to convert each string in the 'split_list' to an integer, and store the result in a new list called 'int_list'.
- Create a tuple from the 'int_list' using the tuple() function, and append it to the 'result' list.
- After the for loop is completed, print the 'result' list.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of
# Convert K delim Strings to Integer Tuple List
# Using for loop and append() method
# initializing list
test_list = ["1-2", "3-4-8-9", "4-10-4"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = "-"
# initializing empty list for result
result = []
# for loop and append method
for sub in test_list:
# split each string by K delimiter
split_list = sub.split(K)
# map() to convert split strings to integers
int_list = list(map(int, split_list))
# create tuple from int_list and append to result
result.append(tuple(int_list))
# printing result
print("The converted tuple list : " + str(result))
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time complexity: O(nm), where 'n' is the number of strings in the input list and 'm' is the average number of integers in each string.
Auxiliary space: O(nm), for the 'result' list that stores the converted tuples.
Method #4: Using regex and for loop
We can also use regular expressions to split the strings at the delimiter and convert the resulting strings to integers using a for loop and the append() method.
- Import the 're' module for working with regular expressions.
- Initialize a list named 'test_list' with some sample data that contains strings separated by a delimiter '-'.
- Print the original list to show the input data.
- Initialize a variable named 'K' with the delimiter used to separate the strings.
- Create a regular expression pattern using the 're.compile()' method that matches any sequence of one or more digits.
- Initialize an empty list named 'res' to store the resulting tuples.
- Iterate through each string in the 'test_list' using a 'for' loop.
- Use the 'pattern.findall()' method to find all the substrings in the current string that match the pattern.
- Convert each substring to an integer using a 'for' loop and the 'int()' method.
- Combine the resulting integers into a tuple using the 'tuple()' method.
- Append the tuple to the 'res' list.
- Print the final 'res' list to show the converted tuple list.
Python3
import re
# initializing list
test_list = ["1-2", "3-4-8-9", "4-10-4"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K and regex pattern
K = "-"
pattern = re.compile(r'\d+')
# initializing result list
res = []
# for loop and append() method
for sub in test_list:
sub_list = pattern.findall(sub)
sub_tuple = tuple(int(num) for num in sub_list)
res.append(sub_tuple)
# printing result
print("The converted tuple list : " + str(res))
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time Complexity: The time complexity of this method is O(n*m), where n is the length of the input list and m is the average length of each string in the input list.
Auxiliary Space: The auxiliary space used by this method is O(n*m), as we are creating a new list to store the resulting tuples.
Method #6: Using a lambda function with map() and split()
In this approach, we can use a lambda function with map() and split() to split each element of the given list by the separator "-" and convert the resulting sublists of strings into tuples of integers.
Step-by-step approach:
- Initialize the given list.
- Define a lambda function that takes a string as input, splits it by "-", and maps the resulting sublists of strings to tuples of integers using the map() function.
- Apply the lambda function to each element of the given list using the map() function.
- Convert the resulting map object to a list using the list() function and store it in a variable.
- Print the resulting list.
Below is the implementation of the above approach:
Python3
# initializing list
test_list = ["1-2", "3-4-8-9", "4-10-4"]
# printing original list
print("The original list is : " + str(test_list))
# define lambda function
split_and_convert = lambda s: tuple(map(int, s.split("-")))
# apply lambda function to each element of the list
result = list(map(split_and_convert, test_list))
# printing result
print("The converted tuple list : " + str(result))
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time complexity: O(nm), where n is the length of the given list and m is the maximum number of elements in any sublist after splitting.
Auxiliary space: O(nm), where n is the length of the given list and m is the maximum number of elements in any sublist after splitting.
Similar Reads
Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
Python program to Convert a elements in a list of Tuples to Float Given a Tuple list, convert all possible convertible elements to float. Input : test_list = [("3", "Gfg"), ("1", "26.45")] Output : [(3.0, 'Gfg'), (1.0, 26.45)] Explanation : Numerical strings converted to floats. Input : test_list = [("3", "Gfg")] Output : [(3.0, 'Gfg')] Explanation : Numerical str
5 min read
Convert List of Tuples to List of Strings - Python The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings.For example, giv
3 min read
Python program to convert tuple into list by adding the given string after every element Given a Tuple. The task is to convert it to List by adding the given string after every element. Examples: Input : test_tup = (5, 6, 7), K = "Gfg" Output : [5, 'Gfg', 6, 'Gfg', 7, 'Gfg'] Explanation : Added "Gfg" as succeeding element. Input : test_tup = (5, 6), K = "Gfg" Output : [5, 'Gfg', 6, 'Gfg
5 min read
Convert Set of Tuples to a List of Lists in Python Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list
3 min read