Python f-string con diccionario literal [duplicado]

Dec 30 2020

¿Cómo hacer un diccionario literal dentro de las llaves f-string?

d_all = {d:d+2 for d in range(10)}
keys = [2,5]
tmp_dict = {d:d_all[d] for d in keys}
print(f'values: {tmp_dict}')

print(f'keys: {[d for d in keys]}')  # list works
print(f'dict: { {{d:d_all[d] for d in keys}} }'  # ???

Respuestas

2 AWhiteFox Dec 29 2020 at 23:14

Prueba esto:

print(f'dict: { {d:d_all[d] for d in keys} }')