144k views
4 votes
Write a Python function that does the following:

1.Its name is all_mappings
2. It takes a single dictionary as an argument, which maps strings to lists of integers
3. It returns a list of 2-value tuples, where the first value of the tuples is a key from D, and the second value is an element from its associated list value

User Pepuch
by
8.0k points

1 Answer

6 votes

Final answer:

The Python function all_mappings returns a list of 2-value tuples created from a given dictionary that maps strings to lists of integers.

Step-by-step explanation:

To achieve the task described, we'll write a Python function named all_mappings. This function will take a single dictionary as its input, where the keys are strings and the values are lists of integers. It will return a list of 2-value tuples, each consisting of a key from the dictionary and one of the integers from its associated list.

Here's a Python function that implements this functionality:

def all_mappings(D):
result = []
for key, value_list in D.items():
for value in value_list:
result.append((key, value))
return result

This function iterates through each key-value pair in the dictionary. For each key, it goes through the corresponding list of integers, creating a tuple with the key and each individual integer, which are then added to the result list.

User Shady Boshra
by
7.2k points