Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
Convert multiple lists with missing value into one dictionary

I am trying to add multiple lists into a dictionary - one key and one value list. I can do two lists to a dictionary but I am wondering how to do multiple lists joining with some missing value in one of the lists.

a = ['apple', 'orange', 'lemon']
b = ['London', 'New York', 'Tokyo']
c = ['Red', 'Orange', 'Yellow']
d = ['good', 'bad', '']

fruito = zip(a, b)
fruit_dictionary = dict(fruito); fruit_dictionary

Expected output:

fruit_dictionary = {'apple': ['London', 'Red', 'good'],
                    'orange': ['New York', 'Orange', 'bad'],
                    'lemon': ['Tokyo', 'Yellow']}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use the following dictionary comprehension with zip and argument packing:

{k:v for k, *v in zip(a,b,c,d)}

output:

{'apple': ['London', 'Red', 'good'],
 'orange': ['New York', 'Orange', 'bad'],
 'lemon': ['Tokyo', 'Yellow', '']}

Or to get rid of empty strings:

{k:[e for e in v if e]      # using truthy values here, could also use e != ''
 for k, *v in zip(a,b,c,d)}

output:

{'apple': ['London', 'Red', 'good'],
 'orange': ['New York', 'Orange', 'bad'],
 'lemon': ['Tokyo', 'Yellow']}

lists of unequal lengths

For completeness, I am adding here a generic solution in case the input lists have different lengths. The solution would be to use itertools.zip_longest:

a = ['apple', 'orange', 'lemon']
b = ['London', 'New York', 'Tokyo']
c = ['Red', 'Orange', 'Yellow']
d = ['good', 'bad']

from itertools import zip_longest
{k:[e for e in v if e] for k, *v in zip_longest(a,b,c,d)}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda