42

I'm removing an char from string like this:

S = "abcd"
Index=1 #index of string to remove
ListS = list(S)
ListS.pop(Index)
S = "".join(ListS)
print S
#"acd"

I'm sure that this is not the best way to do it.

EDIT I didn't mentioned that I need to manipulate a string size with length ~ 10^7. So it's important to care about efficiency.

Can someone help me. Which pythonic way to do it?

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80

5 Answers5

79

You can bypass all the list operations with slicing:

S = S[:1] + S[2:]

or more generally

S = S[:Index] + S[Index + 1:]

Many answers to your question (including ones like this) can be found here: How to delete a character from a string using python?. However, that question is nominally about deleting by value, not by index.

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • That's the most efficient way? Seems so not pythonic. It's works like I wanted. – Alvaro Silvino Jun 28 '16 at 04:09
  • 1
    Why do you care about most efficient way? What is the size of the string and how often do you remove a character from it? – awesoon Jun 28 '16 at 04:11
  • 3
    @AlvaroJoao. There is no "efficient" way to remove elements from a string. A string in Python is immutable. This means that you are going to have to create temporary containers and a new final string regardless. If you want efficient, you can do something like extend `list` and implement a custom `__str__` method. – Mad Physicist Jun 28 '16 at 04:17
  • String size 10^7. The problem is about finding palindromes and remove char til the string becomes a palindrome So I guess I need to care about efficient. – Alvaro Silvino Jun 28 '16 at 04:18
  • 2
    @AlvaroJoao then keep a list of characters instead of a string, a list is mutable and doesn't require creating copies to change. – Tadhg McDonald-Jensen Jun 28 '16 at 04:19
  • @AlvaroJoao. Consider using a `numpy` array for something that size. If you care about efficient, you should not be using pure Python in the first place. – Mad Physicist Jun 28 '16 at 04:20
  • @MadPhysicist thanks for the tips!! It helped me a lot. – Alvaro Silvino Jun 28 '16 at 04:23
  • Here is something you may want to look at: http://stackoverflow.com/q/9476797/2988730. Note that while the storage for arrays is immutable, you are free to shift elements left or use a mask. For 10^7 I would probably do the latter. – Mad Physicist Jun 28 '16 at 04:23
  • @TadhgMcDonald-Jensen yep!! Great suggestion. I will follow this tip to solve this problem! Thanks ! – Alvaro Silvino Jun 28 '16 at 04:24
  • Every time you modify the list size, you are calling `realloc` under the hood. Be careful about how you use lists too. – Mad Physicist Jun 28 '16 at 04:26
  • @AlvaroJoao, Not sure if holding string in a list or array will be enough to solve the problem. 10^7 is too long for brute-force solution. This depends on the full problem statement, though. – awesoon Jun 28 '16 at 04:30
9

Slicing is the best and easiest approach I can think of, here are some other alternatives:

>>> s = 'abcd'
>>> def remove(s, indx):
        return ''.join(x for x in s if s.index(x) != indx)

>>> remove(s, 1)
'acd'
>>> 
>>> 
>>> def remove(s, indx):
        return ''.join(filter(lambda x: s.index(x) != 1, s))

>>> remove(s, 1)
'acd'

Remember that indexing is zero-based.

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
7

You can replace the Index character with "".

str = "ab1cd1ef"
Index = 3
print(str.replace(str[Index],"",1))
min2bro
  • 4,509
  • 5
  • 29
  • 55
1
def missing_char(str, n):

  n = abs(n)
  front = str[:n]   # up to but not including n
  back = str[n+1:]  # n+1 through end of string
  return front + back
Community
  • 1
  • 1
Kate Kiatsiri
  • 63
  • 2
  • 7
0
S = "abcd"
Index=1 #index of string to remove
S = S.replace(S[Index], "")
print(S)

I hope it helps!