简体   繁体   中英

Remove extra bracket from list

In Python, I have this list:

results =  [[['New','York','intrepid', 'bumbling']], [['duo', 'deliver', 'good', 'one']]]

How do I make it the following?

results =  [['New', 'York', 'intrepid', 'bumbling'],['duo', 'deliver', 'good', 'one']]

Here is the solution:

new_list = [x[0] for x in results]
print (new_list)

So what you got here is a list of lists which contains a single list.

My approach would be a list comprehension as follows:

results =  [[['New', 'York','intrepid', 'bumbling']],[['duo', 'deliver', 'good', 'one']]]
results_mod = [list(*x) for x in results]

This iterates over all lists of one list and unpacks it. Since it only contains one list, the list() call does not change anything and the result will be:

results_mod = [['New', 'York', 'intrepid', 'bumbling'], ['duo', 'deliver', 'good', 'one']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM