Open In App

Python – Remove space between tuple elements

Sometimes, while working with Tuples, we can have a problem in which we need to print tuples, with no space between the comma and next element, which by convention, is present. This problem can have use in day-day and school programming. Let’s discuss certain ways in which this task can be performed.
 

Input : test_tuple = (7, 6, 8) 
Output : (7, 6, 8)
Input : test_tuple = (6, 8) 
Output : (6, 8) 
 



Method #1 : Using str() + replace() 
The combination of above functions can be used to solve this problem. In this, we perform the task of removing the additional space, by replacing with empty space.
 




# Python3 code to demonstrate working of
# Remove space between tuple elements
# Using replace() + str()
 
# initializing tuples
test_tuple = (4, 5, 7, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Remove space between tuple elements
# Using replace() + str()
res = str(test_tuple).replace(' ', '')
 
# printing result
print("The tuple after space removal : " + str(res))

Output : 

The original tuple : (4, 5, 7, 6, 8)
The tuple after space removal : (4, 5, 7, 6, 8)

 

 
Method #2 : Using join() + map() 
Another method to solve this problem. In this, we perform the task of removal of space by external joining of each element using join() and extending the logic of string conversion to each element using map().
 




# Python3 code to demonstrate working of
# Remove space between tuple elements
# Using join() + map()
 
# initializing tuples
test_tuple = (4, 5, 7, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Remove space between tuple elements
# Using join() + map()
res = "(" + ", ".join(map(str, test_tuple)) + ")"
 
# printing result
print("The tuple after space removal : " + str(res))

Output : 
The original tuple : (4, 5, 7, 6, 8)
The tuple after space removal : (4, 5, 7, 6, 8)

 

Method #3: Using regex

We can use regular expressions to match and remove the spaces between tuple elements.

Step by step Algorithm:

  1. Initialize a tuple, “test_tuple”, with some elements.
  2. Print the original tuple.
  3. Use the “re.sub()” method to remove spaces between tuple elements.
  4. Pass the regex pattern “\s+” as the first argument to the “re.sub()” method. This pattern matches one or more occurrences of whitespace characters (e.g., spaces, tabs, newlines).
  5. Pass an empty string as the second argument to the “re.sub()” method to replace each match with an empty string.
  6. Convert the resulting string back to a tuple using the “tuple()” method.
  7. Print the tuple after space removal.




# Python3 code to demonstrate working of
# Remove space between tuple elements
# Using regex
 
import re
 
# initializing tuples
test_tuple = (4, 5, 7, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Remove space between tuple elements
# Using regex
res = re.sub(r'\s+', '', str(test_tuple))
 
# printing result
print("The tuple after space removal : " + str(res))

Output
The original tuple : (4, 5, 7, 6, 8)
The tuple after space removal : (4,5,7,6,8)

Time complexity: O(n), where n is the length of the tuple. This is because the “re.sub()” method takes linear time to search for whitespace characters in the input string.

Auxiliary Space: O(n), where n is the length of the tuple. This is because the resulting string has the same length as the input tuple

Method #4: Using a list comprehension and join() method:




# initializing tuples
test_tuple = (4, 5, 7, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Remove space between tuple elements
# Using list comprehension + join()
res = ''.join([str(i) for i in str(test_tuple) if i != ' '])
 
print("The tuple after space removal : " + str(res))

Output
The original tuple : (4, 5, 7, 6, 8)
The tuple after space removal : (4,5,7,6,8)

Time Complexity: O(n), where n is the length of the tuple. This is because the “re.sub()” method takes linear time to search for whitespace characters in the input string.

Auxiliary Space: O(n), where n is the length of the tuple. This is because the resulting string has the same length as the input tuple

Method #5: Using numpy:




import numpy as np
 
# initializing tuples
test_tuple = (4, 5, 7, 6, 8)
 
# convert the tuple to a NumPy array
test_array = np.array(test_tuple)
 
# use NumPy's array_str() function to convert the array to a string
# with the desired format
res = np.array2string(test_array, separator=',', formatter={'int': lambda x: str(x)})
 
# convert the string representation of the array back to a tuple
res_tuple = tuple(map(int, res.strip('[]').split(',')))
 
# print the result
print("The tuple after space removal : " + str(res_tuple))
#This code is contributed by Jyothi pinjala

Output:
The tuple after space removal : (4, 5, 7, 6, 8)

The time complexity : O(n), where n is the length of the input tuple, because we convert the input tuple to a NumPy array and then back to a tuple, both of which take O(n) time.

The auxiliary space: O(n), because we create a NumPy array and a string representation of it, both of which take up additional space in memory.


Article Tags :