Registrations Closing Soon for DevOps Certification Training by CloudxLab | Registrations Closing in

  Enroll Now

Tuples and Dictionaries

There is a function with the name items associated with dictionaries that returns a list of tuples, where each tuple is a key-value pair:

d = { "one" : 1, "two" : 2, "three" : 3}
k = d.items()

It prints dict_items([('one', 1), ('two', 2), ('three', 3)]). We can cast to list using list,

l = list(k)

It prints a list of tuples [('one', 1), ('two', 2), ('three', 3)].

Since it is a dictionary, the items are in no particular order.

However, since the list of tuples is a list, and tuples are comparable, we can now sort the list of tuples. Converting a dictionary to a list of tuples is a way for us to output the contents of a dictionary sorted by key.

We can print the content of the dictionary with below code

for key, value in list(k):
    print(key, value)

It prints,

one 1
two 2
three 3
  • Define a function with the name dict_tuple_func that takes a list of integers as an argument.
  • It should create a dictionary with the key as the integer and the value as the number of times it is present in the list.
  • It should return a tuple in which the first element is the dictionary (you created above step) and the second element is the sum of all unique integers in the list

Sample Input -

dict_tuple_func([1, 2, 3, 1, 2, 3])

Sample Output -

({1: 2, 2: 2, 3: 2}, 6)

Please note output is the tuple in which first element is the dictionary and the second element is the sum of all the unique integers in the list.

No hints are availble for this assesment

Answer is not availble for this assesment

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...