Remove brackets and appostrophes from the list

Hello All…I got a simple request, until now cant find solution from google.
I want to remove rackets and appostrophes from the list.
Ex. list = [3,4,5,]
result is = 3,4,5

I tried copy many code but the best i got is = ‘3,4,5’
Please help…thanks

The square brackets and the commas are a requirement for valid Python list object.

To access the elements in such an object; there are a few ways, one of which, and possibly the most widely used, is to iterate over the list object using a loop:

number_list = [3,4,5]
for number in number_list:
1 Like

Can we do it without loop? Like using list comprehension…am not sure…thanks

List comprehension is a way of creating a list object from some other iterable object and as such (in your example) you’d be no further forward.

You could do the likes of:

number_list = iter([3, 4, 5])


… which will print each number with each print(next(number_list)), but that would require some control code so that you don’t run past the end of the list. Also, that’s a one time thing; that is to say, you can’t iterate over that list more than once per code run. There are ways around that, but it’s more of a corner case.

Maybe you could post the output you’re looking for? That way I can show you how at achieve that.

1 Like

If you want a way without loop and with list comprehension

list = [3, 4, 5]
result = " ".join(str(i) for i in list) # convert list element to string and join them with space

# output
3 4 5

Not that I want to pick at nits; but given that result is a string object, should what you’ve coded not be called ‘string comprehension’?

its a list that is the active object going into the string join() method.

In this case, technically a generator.

1 Like