Skip to content
Advertisement

Python: how to remove key from list and keep value?

I have an array like this

myarr = [
[{'text':'da','id':'aa','info':'aaa'},{'text':'da','id':'aa','info':'aaa'},{'text':'da','id':'aa','info':'aaa'}],
[{'text':'da','id':'aa','info':'aaa'},{'text':'da','id':'aa','info':'aaa'},{'text':'da','id':'aa','info':'aaa'}]
]

I need result:

myarr = [
[['da','aa','aaa'],['da','aa','aaa'],['da','aa','aaa']],
[['da','aa','aaa'],['da','aa','aaa'],['da','aa','aaa']]
]

How can i get sample result? Please help me!

Advertisement

Answer

You can try a list comprehension –

# l will iterate over each inner list and
# e will iterate over dictionaries in each inner list
myarr = [[list(e.values()) for e in l] for l in myarr]
print(myarr)

Ouput:

[[['da', 'aa', 'aaa'], ['da', 'aa', 'aaa'], ['da', 'aa', 'aaa']], [['da', 'aa', 'aaa'], ['da', 'aa', 'aaa'], ['da', 'aa', 'aaa']]]
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement