Jananath Banuka
Jananath Banuka

Reputation: 633

Python: How to print a list without quotations and square brackets

I am trying to print this python list as in the format value1, value1, value3.

But what I get is something like this:

['value1', 'value2', 'value3']

So I want to get rid of ' and [].

This is what I tried, but it now prints the result without only brackets:

tagList = []
count = 0
for i in range(20):
    try:
        if not response['TagList'][i]['Key']:
            print("Tags not found")
        else:
            if str(response['TagList'][i]['Key']) == "t_AppID":
                pass
            tagList.append(str(response['TagList'][i]['Key']))
    except:
        pass

return str(tagList)[1:-1]

Upvotes: 1

Views: 104

Answers (2)

s3dev
s3dev

Reputation: 9681

To return list elements as a string you can use something like:

def as_string(values):
    return ', '.join(values)

print(as_string(['a', 'b', 'c'])

Output:

a, b, c

Upvotes: 0

Thomas
Thomas

Reputation: 181745

Use the join function on str:

>>> mylist = ['value1', 'value2', 'value3']
>>> print(mylist)
['value1', 'value2', 'value3']
>>> print(', '.join(mylist))
value1, value2, value3

Upvotes: 4

Related Questions