![]() Then as we loop through num_list, which will be the list passed as an argument to the function, using a for loop, we are creating key:value pairs in count_dict. Within the function, we first create an empty dictionary, count_dict. We first define the function count() with one parameter, num_list. ![]() To convert this list of tuples into a dictionary, we can use the built-in dict() function: sorted_dictionary = dict(sorted_age) print(sorted_dictionary) # Notice how the sorted() function returns a list of tuples, sorted by the age (or second element in each tuple). For example, to sort by the second element in each tuple, which would be the age, we can use the following code: sorted_age = sorted(dictionary_of_ems(), key = lambda kv: kv) print(sorted_age) # We can sort this dict_items object the same way we sorted the list of tuples seen earlier. Thus, it can be passed in as the iterable to the sorted() function. ![]() ![]() Notice how the items() method returns a dict_items object, which looks similar to a list of tuples. If we want to get a sorted copy of the entire dictionary, we need to use the dictionary items() method: print(dictionary_of_ems()) # dict_items() ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |