I need to remove Square Brackets from list of list (list contains sub list's)
list = [["'deliver","'ordered","'delivery","'delivering","'brought"],["'deliver","'offering","'providing","'delivers","'orders"]]
Sample output
list = 'deliver','ordered','delivery','delivering','brought'
CodePudding user response:
Play with list comprehension, thats what its there for
[print(i) for i in [','.join(x).replace("'",'').split(',') for x in lst]]
['deliver', 'ordered', 'delivery', 'delivering', 'brought']
['deliver', 'offering', 'providing', 'delivers', 'orders']
Or you could just list slice after you edit using list comprehension
[','.join(x).replace("'",'').split(',') for x in lst][0]
CodePudding user response:
set will remove doublicates if exist in your nested list
_list = [["'deliver","'ordered","'delivery","'delivering","'brought"],["'deliver","'offering","'providing","'delivers","'orders"]]
print(', '.join([*set([item for sublist in _list for item in sublist])]))
as normal way
print(', '.join([item for sublist in _list for item in sublist]))
CodePudding user response:
Do you want duplicates or not? It's unclear from the question.
If you don't care about having duplicates, use a list comprehension:
print([string.strip("\'") for sublist in lst for string in sublist])
If you don't want duplicates, then use a set comprehension and convert it back to a list:
print(list({string.strip("\'") for sublist in lst for string in sublist}))
In both examples, we remove the inner single quotation from each string using the strip method, and once it is removed, python automatically converts the double quotes to single quotes. However, the fact that there are double quotes or single quotes doesn't matter, the items in the resulting lists still have type string. What kind of quotations only matters in cases mentioned here: