skr3178
skr3178

Reputation: 7

How to remove ' from a list with several items

Here is the code

x = (([(''+ 'e[' + str(i) + ']') for i in range(11)]))
print(x)
['e[0]', 'e[1]', 'e[2]', 'e[3]', 'e[4]', 'e[5]', 'e[6]', 'e[7]', 'e[8]', 'e[9]', 'e[10]']


In the end I want the format as TUPLE To remove '' you could convert this to a string:

y = ', '.join(x)
print(y)
e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], e[8], e[9], e[10]

Upvotes: 0

Views: 55

Answers (2)

typh
typh

Reputation: 11

The single quotes are there because each element of the list x is a string - if you want to produce a string representation of x with single quotes omitted, the following should work:

str(x).replace("'","") # == '[e[0], e[1], e[2], e[3], ..., e[10]]'

If you're looking to display x as a tuple, you could convert it first as well - e.g:

str(tuple(x)).replace("'","") # == '(e[0], e[1], e[2], e[3],..., e[10])'

Upvotes: 0

Rafael Valero
Rafael Valero

Reputation: 2816

Perhaps this?:

x = (([(''+ 'e[' + str(i) + ']') for i in range(11)]))

x looks like this: ['e[0]', 'e[1]', 'e[2]', 'e[3]', 'e[4]', 'e[5]', 'e[6]', 'e[7]', 'e[8]', 'e[9]', 'e[10]']

To remove the '' perhaps:

y = ', '.join(x)
print(y)
e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], e[8], e[9], e[10]

To make the string looks like a tuple:

z = '('+', '.join(x)+')'
print(z)
(e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], e[8], e[9], e[10])

Upvotes: 1

Related Questions