Open In App

Python – Remove space between tuple elements

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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




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

  • Create a list comprehension
  • Filter out the spaces from the list comprehension using an if statement:
  • Use the join() method to concatenate the filtered list into a single string:
  • Assign the concatenated string to the variable res.
  • Print the original tuple and the modified tuple string representation.

Python3




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

  • Convert the input tuple to a NumPy array using np.array() function.
  • Use the np.array2string() function to convert the NumPy array to a string representation with the desired separator and format. The resulting string will contain square brackets instead of parentheses.
  • Remove the square brackets from the string representation using the strip(‘[]’) method.
  • Split the string on commas using the split() method, which will produce a list of strings representing the tuple elements.
  • Use the map() function to apply the int() function to each element of the list of strings, converting them to integers.
  • Convert the resulting list of integers to a tuple using the tuple() constructor.

Python3




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.



Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads