I have two types of datas like the following:
data1 ={
'ArticulosAutores': [],
'LibrosAutores': [],
'Articulos': [
{
'abstract': 'El artículo hace una reseña',
'keywords': '',
'indice': 'Artículos Publicados de Divulgación Local',
'fecha_publicacion': '2005-02-04',
'mapeo': [
'Abstract',
'Keywords',
'Indice',
'Fecha Publicacion'
]
}
],
'Libros': [],
'Proyectos': [
{'fecha_inicio': '2005-02-01',
'codigo_proyecto': '',
'descripcion': 'Asadsadsadsadsa sadsadsadsa',
'mapeo': [
'Fecha Inicio',
'codigo_proyecto',
'descripcion',
]
}
]
}
data2 = [
'ArticulosAutores',
'LibrosAutores',
'Articulos',
'Libros',
'Proyectos',
]
My question is, how can I eliminate from data2
the lists that are empty in data1
for example ArticulosAutores LibrosAutores
and Libros
are empty in data1
then based on that in data2
obtaining a result like the following:
data2 = ['Articulos', 'Proyectos']
I hope you can help me. Thanks in advance.
Very easy, we just go through the dictionary and choose the elements whose value is not empty, for that we can do a list compression or a dictionary compression, I will show both examples.
dictionary compression
Result
list compression
Result
As we can see, it returns a dictionary or list with all the values that are not empty lists.