1

I have a list that looks like this ["first letter", " second letter", " third letter"]In some cases, the first character is an empty character " ". In these cases, I want to strip off the first character and modify my list to become like this:

["first letter", "second letter", "third letter"]

How can I achieve this? This doesnt work because I am not modifying the string itself and there can be more than 1 " " characters in each string. I only want to remove the first.

    for i in setOfCoordinates:
        if i[0] == ' ':
            print('space found')
            i.replace(" ", "")

I don't want to remove all spaces, just the first one

10
  • for i in setOfCoordinates:. You are already iterating over the elements of the list. Fetching i[0] will fetch the first character of the element. Replace i[0] with i Commented Aug 6, 2021 at 8:56
  • test = [i.lstrip() for i in setOfCoordinates] removes all spaces also. :) Commented Aug 6, 2021 at 8:57
  • That is intentional. I want to fetch the first character and check if its an empty character. If its empty, then I want to strip it off @Sujay Commented Aug 6, 2021 at 8:58
  • I don't want to remove all spaces, just the first one @user56700 Commented Aug 6, 2021 at 8:59
  • [i.lstrip() for i in a] should do the job Commented Aug 6, 2021 at 9:02

5 Answers 5

2
my =  ["first letter", " second letter", " third letter"]
for i in range(0,len(my),1):
    if my[i][0] == " " :
        my[i] = my[i][1:]
print(my)

# result : ['first letter', 'second letter', 'third letter']

I used the 'for' function. After that, through 'Slice', the string was truncated if the front was blank.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this. Use enumerate. Also, replace is not an in place function. It will return the replaced string. So, you can use string concatenation. i[0]+i[1:] which concatenate the string.

l=["first letter", " second letter", " third letter"]
for j,i in enumerate(l):
    if i[0]==" ":
        print("Space found")
        l[j]=i[0].replace(" ",'')+i[1:]
        print("Space removed")
print(l)

Output:

Space found
Space removed
Space found
Space removed
['first letter', 'second letter', 'third letter']

Comments

0

Just use the count parameter of the replace function: https://docs.python.org/3/library/stdtypes.html#str.replace

In your case:

i.replace(" ", "", 1)

just replaces 1 occurrence of " "

In order to modify the strings in the list, use their index (credit to @Sujay answer):

l = ["first letter", " second letter", " third letter"]
for j, i in enumerate(l):
    if i[0] == " ":
        print("Space found")
        l[j] = l[j].replace(" ", "",1)
print(l)

2 Comments

but if the first char isn't a space, it would remove the middle space, no?
In that case yes, but first you check if the first character is a space, then apply the replace
0

You are looking for str.lremoveprefix() if you want to remove the first space but not the spaces after the letters. See more info on https://docs.python.org/3/library/stdtypes.html

setOfCoordinates = [" first letter", "  second letter  ", "   third letter   "]
print(setOfCoordinates) #[' first letter', '  second letter  ', '   third letter   ']
setOfCoordinates = [i.removeprefix(" ") for i in setOfCoordinates]
print(setOfCoordinates) #['first letter', ' second letter  ', '  third letter   ']

4 Comments

but doesn't modify the original list
@x89 code is now updated and now modify the original list
lstrip strips all leading spaces, not just one
@SevC_10, my bad, its str.removeprefix() we are looking for here. Updating the answer
-1
x = ["first letter", " second letter", " third letter"]
x = [i.strip() for i in x] #### remove space from both the ends
print(x)

removing space from the start

x = ["first letter", " second letter", " third letter"]
x = [i.lstrip() for i in x]
print(x)

removing space from end

x = ["first letter", " second letter", " third letter"]
x = [i.rstrip() for i in x] #### remove space from both the ends
print(x)

2 Comments

strip functions remove all trailing or leading spaces, not just one
That's why I have shown for all. Whichever he wants to use can use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.