Change format of a list when printed in python

Viewed 47

I am pulling a Query set and putting into a list as below -

data = Data.objects.all().values_list('number')
data_list = list(data)
print(data_list)

[('1',), ('2',), ('3',)]

But I would like to change this to be formatted as a list without the brackets like below -

[1,2,3]

How can I do this?

2 Answers

You can work with flat=True to the .values_list(…) [Django-doc], to make a list of scalars instead of tuples:

Data.objects.values_list('number', flat=True)

You can make use of list comprehension:-

val=[(str(list(x)[0])) for x in val]

Now print val you will get your desired output:-

[1,2,3]
Related