Avatar ·

How to remove all spaces from a string in Python?

📁 function, example

I googled the function strip(), but it only removes the first and last space, and I need all of them. For example, if a = ' sd dfsdf dfsfs', then I need to get a = 'sddfsdfdfsfs'. Is there such a function or do I need to do it through a loop somehow?

Avatar ·

In functional style in one line:

reduce(lambda a, x: a + x, map(lambda x: list(x), ['1', '22', '333']))

I'll explain - reduce takes a function that combines the results of splitting the list by the map function. Map takes a function and an initial array, and the given function is executed on each element of the array. The same thing can be expressed with the following code:

>>> result_list = []>>> for x 
                            

Log in to leave an answer

Blogs