logo CodeStepByStep logo

collection_mystery5

Language/Type: Python collection mystery collections

Write the output that is printed when the given function below is passed each of the following maps and lists as its parameters. Recall that maps prin a {key1=value1, key2=value2, ..., keyN=valueN} format.

Though hash maps usually have unpredictable ordering, for this problem, you should assume that when looping over the map or printing a map, it visits the keys in the order that they were added to the map or the order they are declared below. If a map calls put() on a key that already exists, it retains its current position in the ordering (as would be the case for an OrderedDict).

def collection_mystery5(list1, list2):
    result = {}

    for i in range(len(list1)):
        s1 = list1[i]
        s2 = list2[i]

        if not s1 in result:
            result[s1] = s2
        elif not s2 in result:
            result[s2] = s1
        else:
            result[s1 + s2] = s1
    print(result)
list1 = ['cat', 'cat', 'long', 'long', 'longcat'] list2 = ['mew', 'purr', 'cat', 'cat', 'purr' ]
list1 = ['a', 'b', 'a', 'ab', 'ab', 'y', 'abb'] list2 = ['b', 'c', 'b', 'b', 'c', 'abb', 'y' ]

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.