I have a data like this in which I have a dictionary with lists that have dictionaries:
bloqueAtributos = {
"Articulos":[
{
"id":3042,
"titulo":"Propuesta de Investigación Socioeducativa de la UTPL",
"keywords":"",
"link_articulo":""
},
{
"id":3043,
"titulo":"Prueba",
"keywords":"pruebas",
"link_articulo":""
},
{
"id":3044,
"titulo":"Prueba",
"keywords":"pruebas",
"link_articulo":""
}
],
"Libros":[
],
"Proyectos":[
{
"codigo_proyecto":""
},
{
"codigo_proyecto":""
}
],
"GradoAcademico":[
],
"Capacitacion":[
{},
{}
]
}
And I'm trying to delete empty lists and lists with empty dictionaries, for which I do this:
bloquesInfoRestante = {k:v for k,v in bloqueAtributos.items() if v !=[] and v !=[{}]}
print(bloquesInfoRestante)
With that I can eliminate the lists that are empty. But not the lists that are found with more than one empty dictionary.
With what I have now it returns me this:
bloquesInfoRestante = {
"Articulos":[
{
"id":3042,
"titulo":"Propuesta de Investigación Socioeducativa de la UTPL",
"keywords":"",
"link_articulo":""
},
{
"id":3043,
"titulo":"Prueba",
"keywords":"pruebas",
"link_articulo":""
},
{
"id":3044,
"titulo":"Prueba",
"keywords":"pruebas",
"link_articulo":""
}
],
"Proyectos":[
{
"codigo_proyecto":""
},
{
"codigo_proyecto":""
}
],
"Capacitacion":[
{},
{}
]
}
As you can see, it only eliminates the empty lists, but not the lists with empty dictionaries. My question is how can I remove the lists with empty dictionaries as well. I hope you can help me. Thanks in advance
You might consider filtering the list
v
with[item for item in v if item != {}]
, so we guarantee that the listv
has at least one non-empty dictionary.If there are dictionaries that have at least one non-empty dictionary like
{'a': [{}, {'foo': 2}], 'b': [], 'c': [{}, {}]}
, itbloquesInfoRestante
would be{'a': [{}, {'foo': 2}]}