How to remove brackets from lists in list using list comprehension?
Question:
I’d like to remove the brackets around my querysets using list comprehension.
This is what I have :
foo = [[<queryset: two>], [<queryset: four>], [<queryset: one>]]
And this is what I want :
bar = [<queryset: two>, <queryset: four>, <queryset: one>]
I tried to use list comprehension like so but it didn’t change anything :
bar = [x for x in foo]
What am I doing wrong ?
Answers:
Try this,
In [1]: foo = [['<queryset: two>'], ['<queryset: four>'], ['<queryset: one>']]
In [2]: [i[0] for i in foo]
Out[2]: ['<queryset: two>', '<queryset: four>', '<queryset: one>']
I’d like to remove the brackets around my querysets using list comprehension.
This is what I have :
foo = [[<queryset: two>], [<queryset: four>], [<queryset: one>]]
And this is what I want :
bar = [<queryset: two>, <queryset: four>, <queryset: one>]
I tried to use list comprehension like so but it didn’t change anything :
bar = [x for x in foo]
What am I doing wrong ?
Try this,
In [1]: foo = [['<queryset: two>'], ['<queryset: four>'], ['<queryset: one>']]
In [2]: [i[0] for i in foo]
Out[2]: ['<queryset: two>', '<queryset: four>', '<queryset: one>']