Sobes.tech
Junior

How to create a dictionary from two lists — one with keys and the other with values?

sobes.tech AI

Answer from AI

Using the zip() function to combine lists and the dict() function to convert pairs into a dictionary. Checking if the list lengths match to avoid errors.

# Example lists
keys = ['apparel', 'electronics', 'footwear']
values = ['clothing', 'electronics', 'shoes']

# Check list lengths
if len(keys) != len(values):
    print("List lengths do not match!")
else:
    # Create dictionary with zip and dict
    result_dict = dict(zip(keys, values))
    
    # Output the result
    print(result_dict)