0
list = ["/x01", "/x30", "/x30", "/x53", "/x46", "/x46", "/x46", "/x30", "/x30", "/x30", "/x31", "/x46", "/x46", "/x0D"]

for x in list:
    x.replace("[","").replace("]","").replace('"','').replace(" ","").replace(",","").replace("[","")

This hasn't been working. Could you explain what I might be doing wrong, and tell me if there is a more effective way of getting the output: "/x01/x30/x30/x53/x46/x46/x46/x30/x30/x30/x31/x46/x46/x0D"

4
  • As a side note, do you really have "/x01", not "\x01"? (And, if it's the latter, do you have the four-character string with a backslash, or a single control-A?) Commented Nov 21, 2013 at 19:12
  • Thank you for noticing that! It is in fact supposed to be with the backslash "\x". The four character string using "\x01" instead of using control-A has been working for me. Commented Nov 22, 2013 at 22:17
  • Hold on a second, I think you're mixing up values and their representations again. The literal "\x01" is not a four-character string, it's a one-character control-A. The representation of that single-character string is the six characters "\x01", but you can verify that the actual string is only one character by, e.g., typing len("\x01") in the interactive interpreter. Also, print("\x01") will print an invisible character, not four characters. Commented Nov 23, 2013 at 1:08
  • And that means if you just print out ''.join(list), So, you're going to get a 14-character string with an invisible character, then 00SFFF0001FF, then a carriage return, not the 56-character string you wanted. Commented Nov 23, 2013 at 1:09

3 Answers 3

3

The first problem is that replace doesn't change a string in-place, it just returns a new string. And you're ignoring that new string.

What you want is:

new_list = []
for x in list:
    new_list.append(x.replace("[","").replace("]","").replace('"','').replace(" ","").replace(",","").replace("[",""))

You can simplify that with translate, or use a different way of filtering out the characters like a comprehension or a filter call. But the result will be the same.


The bigger problem is that what you're trying to do doesn't make any sense. None of the elements in your list have a [, ], ", etc. character in them. You're probably confusing the string representation of the list with the list itself.

If you want to join the members of a list, or to produce any representation of the list other than the default repr, just explicitly join them. For example, this gets what you seem to want:

''.join(list)

… and this gets a different representation:

' and '.join(list)

… and this gets roughly the same thing as repr:

'[' + ', '.join(map(repr, list)) + ']'
Sign up to request clarification or add additional context in comments.

Comments

2

Use str.join:

>>> lis = ["/x01", "/x30", "/x30", "/x53", "/x46", "/x46", "/x46", "/x30", "/x30", "/x30", "/x31", "/x46", "/x46", "/x0D"]
>>> ''.join(lis)
'/x01/x30/x30/x53/x46/x46/x46/x30/x30/x30/x31/x46/x46/x0D'

Looking at your code, I think you were trying to apply str.replace on str version of the list. But that would be a weird way to do this, better use str.join:

>>> str(lis)
"['/x01', '/x30', '/x30', '/x53', '/x46', '/x46', '/x46', '/x30', '/x30', '/x30', '/x31', '/x46', '/x46', '/x0D']"

The above string is just a representation of the list object.

>>> str(lis).replace("[","").replace("]","").replace(" ","").replace(",","").replace("'","")
'/x01/x30/x30/x53/x46/x46/x46/x30/x30/x30/x31/x46/x46/x0D'

2 Comments

In other words: ['a', 'b'] is not a string containing '[' or ',', which the original poster seemingly fails to notice.
Thank you, that is the problem I was facing. I'm pretty new to programming, so after having many times when I already had a string representation of a list at other points in the program, I forgot to do it here...
0

You should use translate(None, nuke_list_string)

Two reasons:

  1. replace performs a 1-to-1 substitution, each substitution results in the allocation of a new string which is then returned, resulting in a lot of overhead if you chain a bunch of replaces like you do in the example.

  2. translate usually requires a 1-to-1 mapping, i.e. translate('ABC','abc') would convert the string 'a brown cow' to 'A Brown Cow'. However, if you specify None as the new mapping, it will remove the characters it finds.

Here's an example:

a = 'this is [a] string with (stuff) in {it}'
print a.translate(None, '[]{}()')

Produces:

this is a string with stuff in it

1 Comment

Thanks - the explanation of how to use translate along with everyone else pointing out my problem with not using .join() should make my code more effective.

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.