Here's a function that creates a dictionary from two lists of different lengths, where the first list contains keys and the second list contains values:
def create_dictionary(keys, values):
dictionary = {}
for i in range(min(len(keys), len(values))):
dictionary[keys[i]] = values[i]
return dictionary
In this function, keys and values are the two input lists. We create an empty dictionary to store the key-value pairs. The for loop iterates over the range of the minimum length between the two lists, ensuring that we only consider the common elements. Within each iteration, we assign the i-th element of keys as the key and the i-th element of values as the corresponding value in the dictionary. Finally, we return the resulting dictionary.
You can use this function as follows:
keys = ['a', 'b', 'c']
values = [1, 2, 3, 4]
result = create_dictionary(keys, values)
print(result)
Output:
{'a': 1, 'b': 2, 'c': 3}
In this example, the keys list contains three elements, and the values list contains four elements. The function will create a dictionary by matching the keys and values until the minimum length is reached. The resulting dictionary is then printed.