Open In App

Python program to convert a list of strings with a delimiter to a list of tuple

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 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))

Output
The 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 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))

Output
The 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:

Below is the implementation of the above approach:




# 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))

Output
The 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.

  1. Import the ‘re’ module for working with regular expressions.
  2. Initialize a list named ‘test_list’ with some sample data that contains strings separated by a delimiter ‘-‘.
  3. Print the original list to show the input data.
  4. Initialize a variable named ‘K’ with the delimiter used to separate the strings.
  5. Create a regular expression pattern using the ‘re.compile()’ method that matches any sequence of one or more digits.
  6. Initialize an empty list named ‘res’ to store the resulting tuples.
  7. Iterate through each string in the ‘test_list’ using a ‘for’ loop.
  8. Use the ‘pattern.findall()’ method to find all the substrings in the current string that match the pattern.
  9. Convert each substring to an integer using a ‘for’ loop and the ‘int()’ method.
  10. Combine the resulting integers into a tuple using the ‘tuple()’ method.
  11. Append the tuple to the ‘res’ list.
  12. Print the final ‘res’ list to show the converted tuple list.




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))

Output
The 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:

Below is the implementation of the above approach:




# 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))

Output
The 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.


Article Tags :