Strings in Python are immutable (meaning that their data cannot be modified) so the replace method doesn't modify the string - it returns a new string. You could fix your code as follows:

for i in hello:
    j = i.replace(' ','')
    k.append(j)

However a better way to achieve your aim is to use a list comprehension. For example the following code removes leading and trailing spaces from every string in the list using strip:

hello = [x.strip(' ') for x in hello]
Answer from Mark Byers on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-remove-multiple-empty-spaces-from-string-list
Python - Ways to remove multiple empty spaces from string List - GeeksforGeeks
July 12, 2025 - Using str.replace() in a loop allows for replacing specific substrings in each string of a list or text. This method iterates through the strings applying the replacement operation for each occurrence of the target substring. ... s = ["Hello world", " Python is great ", " Extra spaces here "] # Remove multiple spaces using replace in a loop b = [string.replace(' ', ' ') for string in s] # Repeat the replace step until no double spaces exist a = [string.replace(' ', ' ') for string in b] print(a)
Discussions

how to remove white spaced from a nested list
Post your code. Also think about using strip instead of replace. It sounds like you are on the right track but "messing with the design of the list" suggests you need some help understanding how you are manipulating it. More on reddit.com
🌐 r/learnpython
10
1
August 10, 2021
Removing space from list items
With Replace (" ", "") But why dit you put numbers as strings in a list? To convert it you can use something like this: list = [' 1', ' 2', ' 4', ' 8'] res = [eval(i) for i in list] print("Modified list is: ", res) More on reddit.com
🌐 r/pythonhelp
3
1
September 14, 2024
python - How to remove all spaces in the strings in list - Stack Overflow
My list is like this, mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"] How to remove all spaces containing strings inside this list? More on stackoverflow.com
🌐 stackoverflow.com
How to remove extra space from output list tuple?
Hi, I have this input [python]P (0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)[/python] and I would like to have it printed out this way [python][(0, 2, 1), (2, 0, -1), (0, 4, 1), (4, 0, -1)][/python]. However, due to the extra space in the input I ran into this error. More on discuss.python.org
🌐 discuss.python.org
3
0
May 5, 2022
🌐
w3resource
w3resource.com › python-exercises › list › python-data-type-list-exercise-206.php
Python: Remove additional spaces in a given list - w3resource
June 28, 2025 - # Define a function 'test' that removes additional spaces from the elements of a given list. def test(lst): # Create an empty list 'result' to store the modified elements. result = [] # Iterate through the elements of the input list 'lst'. for i in lst: # Remove extra spaces in the current element and store it in 'j'. j = i.replace(' ', '') # Append the modified element 'j' to the 'result' list. result.append(j) return result # Define a list 'text' containing strings with extra spaces. text = ['abc ', ' ', ' ', 'sdfds ', ' ', ' ', 'sdfds ', 'huy'] # Print a message indicating the original list
🌐
EyeHunts
tutorial.eyehunts.com › home › how to remove space in list python | example code
How to remove space in list Python | Example code
August 5, 2021 - The first way is using a replace() method with for-loop to remove space in list Python Another better way to achieve it is to use a list comprehension. Simple example code: It will remove space not empty string. list1 = ['A', ' ', ' ', 'B', ...
🌐
Reddit
reddit.com › r/learnpython › how to remove white spaced from a nested list
r/learnpython on Reddit: how to remove white spaced from a nested list
August 10, 2021 -

hi, I have made a list already but wanted to remove the blank spaces from each index of my nested list. the list is pretty long so I will add a small version of it below.

list1 = [[' CITS2224-1 ', ' CITS2023-2 ', ' CITS3221-2 ', ' CITS3224-1 ', ' CITS3225-1 '], [' CITS2023-2 ', ' ENSC3221-2 ', ' ENSC3224-1 ', ' ENSC3225-1 ', ' ENSC3227-2 ', ' ENSC3210-1 ', ' ENSC3221-1 ', ' MECH3023-2 '], [' CITS1021-2 ', ' CITS2021-1 ', ' CITS2023-2 ', ' ENSC2224-1 ', ' ENSC3211-2 ', ' ENSC3213-2 ', ' MATH2221-1 ', ' STAT1524-1 ']

there is a space in front and after each index of the string and I want to remove it. for example the first index being 'CITS224-1'. I have tried looping through the entire list and using the replace function however that is messing with the design of the list. I want to leave the way that the list is nested untouched only want to remove the blank space before and after

thanks for any help

🌐
GeeksforGeeks
geeksforgeeks.org › python-remove-additional-spaces-in-list
Python | Remove additional spaces in list | GeeksforGeeks
March 14, 2023 - Let's look at different methods ... different ways to remove list items in Python.Removing Item by Value with remove()The remove() method allows us to remove the first occurrence o...
🌐
py4u
py4u.org › blog › python-removing-spaces-from-list-objects
Python: How to Remove Spaces from List Objects – Fixing Non-Working Code [Step-by-Step]
strip() already handles all whitespace (spaces, tabs \t, newlines \n, carriage returns \r, etc.), but if you need to remove internal whitespace (not just spaces), use a regex with re.sub().
Find elsewhere
🌐
Iditect
iditect.com › faq › python › python-removing-spaces-from-list-objects.html
Python: Removing spaces from list objects
This snippet uses str.split() and str.join() to remove all spaces from list strings. my_list = [' hello world ', 'Python is great', ' iditect '] cleaned_list = [''.join(item.split()) for item in my_list] print(cleaned_list) # Output: ['helloworld', 'Pythonisgreat', 'iditect']
🌐
Python.org
discuss.python.org › python help
How to remove extra space from output list tuple? - Python Help - Discussions on Python.org
May 5, 2022 - Hi, I have this input [python]P (0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)[/python] and I would like to have it printed out this way [python][(0, 2, 1), (2, 0, -1), (0, 4, 1), (4, 0, -1)][/python]. However, due to the extra space in the input I ran into this error.
🌐
Codecademy Forums
discuss.codecademy.com › computer science
Stripping white space from a list within a list - Computer Science - Codecademy Forums
February 20, 2020 - Hello, I’m going through the Python 3 course, and I got to the “Thread Shed” project under the strings section. https://www.codecademy.com/courses/learn-python-3/projects/thread-shed I’m stuck on task 8, where it asks me to clean up the strings within a list which is, itself, within ...
Top answer
1 of 5
5

I think you are conflating the list and the representation of the list. The list itself doesn't have spaces, or even commas, it just has the items (in your case, the numbers). The representation of the list is just that - a way to represent the data inside of it in a normal, standard way. That standard includes commas and spaces.

If you want a new thing that represents the list and its items in the way you are saying, you can do that by making a new string just like that.

str(numbers).replace(' ','')

This has two functions in it, chained together. First, we do str(numbers) to get a string that is the string-representation of the list. This will be the string '[1, 2, 3, 4, 5]'. Then you replace any blank space-bar ' ' with nothing ''.

Edit:

I think I read your question too quickly and see that you did the exact same as what I have in my code here and said it isn't doing exactly what you want to do. I think the answer here is: no.

As I say in my first paragraph, there is the list and there is the lists representation. The function for the list's representation is a python built-in that can not be trivially overridden. I'm not saying it can't be done, but I don't think you'll get it done without defining some new things.

2 of 5
2

You can change the print behavior of the list to something you code yourself.

numbers = input().split(',')

for i in range(0, len(numbers)):
    numbers[i] = int(numbers[i])
mylist = str(numbers).replace(' ','')

print('[' + ','.join([str(n) for n in numbers]) + ']')

This prints a bracket [, then each number separated by commas without any spaces, and finally the ending bracket ].

Output looks like this:

1, 2, 3, 4, 5
[1,2,3,4,5]
Top answer
1 of 4
2

The strip() method will strip all the characters from the end of the string. In your case, strip starts at the end of your string, encounters a '\n' character, and exits.

It seems a little unclear what you are trying to do, but I will assume that you are looking to clear out any white space between the last non-whitespace character of your string and the newline. Correct me if I'm wrong.

There are many ways to do this, and this may not be the best, but here is what I came up with:

m = ['This, is a string. \n', 'another string!    \n', 'final example\n    ']

m = map(lambda(x): x.rstrip() + '\n' if x[-1] == '\n' else x.rstrip(' '), m)

print(m)

['This, is a string.\n', 'another string!\n', 'final example\n']

Here I use the built in map function iterate over each list element and remove all white space from the end (rstrip() instead of strip() which does both the start and end) of the string, and add in a new line if there was one present in the original string.

2 of 4
1

Your code wouldn't be useful in a script; you are just seeing the REPL displaying the result of the expression i.strip(' '). In a script, that value would just be ignored.

To create a list, use a list comprehension:

result = [i.strip(' ') for i in m if ' ' in i]

Note, however, strip only removes the requested character from either end; in your data, the space precedes the newline. You'll need to do something like removing the newline as well, then put it back:

    result = ["%s\n" % i.strip() for i in m if ' ' in i]
🌐
Stack Overflow
stackoverflow.com › questions › 47385830 › how-to-remove-spaces-from-list-of-lists-in-python
regex - How to remove spaces from list of lists in Python? - Stack Overflow
October 18, 2021 - I have the following input: [ ['A(x) | B(x)'],['Function(x,y) | K(x)'] ] Each list within the list of list has a space before and after the character "|". How can I remove this to get the following