Python Practice › Dictionary Programs
Python Dictionary Programs with Solutions
94 dictionary programs: adding, removing and renaming keys, merging, sorting by key or value, counting with dictionaries, grouping, inverting, and working with nested dictionaries and lists of dictionaries. Every solution is explained with its real output.
Before you start
Read the Dictionaries lesson first if the topic is new to you.
All programs
- #1Add elements to a dictionary
Write a Python program to add elements to a dictionary.Easy - #2Square of every value
Write a Python program to print the square of every value in a dictionary.Easy - #3Move items to another dictionary
Write a Python program to move all items from dict1 to dict2, leaving dict1 empty.Easy - #4Concatenate two dictionaries
Write a Python program to concatenate two dictionaries.Easy - #5Odd and even keys
Write a Python program to get lists of the items with even keys and with odd keys.Easy - #6Dictionary from two lists
Write a Python program to create a dictionary from a list of keys and a list of values.Easy - #7Squares of even, cubes of odd
Write a Python program that stores squares of even numbers and cubes of odd numbers in a dictionary using a comprehension.Easy - #8Clear a dictionary
Write a Python program to remove all items from a dictionary.Easy - #9Remove duplicate values
Write a Python program to keep only the first key for each distinct value.Easy - #10Character counts of a string
Write a Python program to build a dictionary of character counts from a string.Easy - #11Sort a dictionary by key
Write a Python program to sort a dictionary by its keys.Easy - #12Sort a dictionary by value
Write a Python program to sort a dictionary by its values.Easy - #13Add a key
Write a Python program to add a key to a dictionary.Easy - #14Merge two dictionaries with **
Write a Python program to concatenate two dictionaries using unpacking.Easy - #15Swap the values of two keys
Write a Python program to swap the values of two keys.Easy - #16Sum of dictionary values
Write a Python program to add up all the values of a dictionary.Easy - #17Memory size of a dictionary
Write a Python program to get the memory size of a dictionary withsys.getsizeof, and its number of items.Easy - #18Does a key exist?
Write a Python program to check whether a key exists in a dictionary.Easy - #19Iterate over a dictionary
Write a Python program to loop over a dictionary and print each key and value.Easy - #20Dictionary of cubes
Write a Python program to create {k: k³} for k from 1 to n.Easy - #21Insert a key at the beginning
Write a Python program to insert a key at the beginning of a dictionary.Easy - #22Squares of 1 to 5
Write a Python program to create a dictionary with keys 1 to 5 and their squares as values.Easy - #23Product of dictionary values
Write a Python program to multiply all the values of a dictionary.Easy - #24Remove a key
Write a Python program to remove a key from a dictionary.Easy - #25Map two lists to a dictionary
Write a Python program to map a list of keys to a list of values.Easy - #26Max and min values
Write a Python program to find the maximum and minimum values of a dictionary, with their keys.Easy - #27Group equal items
Write a Python program to group equal items of a list into dictionary values.Easy - #28Replace words using a dictionary
Write a Python program to replace words in a string using a dictionary of replacements.Easy - #29Remove words that are dictionary keys
Write a Python program to remove words from a string if they are keys in a dictionary.Easy - #30Keep values that appear once in each list
Write a Python program to remove every value that is repeated inside each list value.Easy - #31Is a dictionary empty?
Write a Python program to check whether a dictionary is empty.Easy - #32Add values of matching keys
Write a Python program to combine two dictionaries, adding the values of keys that appear in both.Easy - #33Unique values in a list of dicts
Write a Python program to print the unique values from a list of dictionaries.Easy - #34Letter combinations from values
Write a Python program to print every combination of one letter from each dictionary value.Easy - #35Letter frequency with Counter
Write a Python program to create a dictionary of letter counts from a string.Easy - #36Print a dictionary as a table
Write a Python program to print a dictionary of lists as a table.Easy - #37Count frequencies in a list
Write a Python program to count how often each item appears in a list, using a dictionary.Easy - #38Mean of dictionary values
Write a Python program to find the mean of a dictionary’s values.Easy - #39List to a nested dictionary
Write a Python program to convert a list into a nested dictionary of keys.Medium - #40Sort the list values
Write a Python program to sort each list value of a dictionary.Easy - #41Product with the highest price
Write a Python program to find the product with the highest price.Easy - #42Print a nested dictionary line by line
Write a Python program to print a nested dictionary line by line.Easy - #43Dictionary of lists to list of lists
Write a Python program to convert {key: [values]} into [[key, values…], …].Easy - #44List of dicts to keys and values
Write a Python program to convert a list of dictionaries into a list of its keys and a list of its values.Easy - #45Count items in list values
Write a Python program to count the total number of items in all list values.Easy - #46Sort items in descending order of value
Write a Python program to sort a dictionary’s items by value, highest first.Easy - #47Replace marks with their average
Write a Python program to replace the numeric marks p1 and p2 with a single average entry.Easy - #48Matching key-value pairs
Write a Python program to find key-value pairs present in both dictionaries.Easy - #49Keys a, b, c with ranges
Write a Python program to create {“a”: 1-5, “b”: 6-10, “c”: 11-15} as lists.Easy - #50Drop empty items
Write a Python program to drop items whose value is None.Easy - #51Filter by value
Write a Python program to keep entries with values greater than 40.Easy - #52Two lists in a dict to one flat dict
Write a Python program to pair the two list values of a dictionary into a flat dictionary.Easy - #53List of tuples to a dictionary
Write a Python program to convert a list of pairs into a dictionary.Easy - #54String to a dictionary
Write a Python program to convert “key=value; key=value” text into a dictionary.Easy - #55Matrix rows to a dictionary
Write a Python program to convert a matrix into a dictionary keyed by row number.Easy - #56Are all values the same?
Write a Python program to check whether all values in a dictionary are the same.Easy - #57Group key-value pairs into lists
Write a Python program to group a sequence of (key, value) pairs into a dictionary of lists.Easy - #58Dict of lists to list of dicts
Write a Python program to split a dictionary of lists into a list of dictionaries.Easy - #59Remove a dictionary from a list
Write a Python program to remove the 4th dictionary from a list.Easy - #60Convert string values to numbers
Write a Python program to convert the string values of a dictionary to int or float.Easy - #61Empty every list value
Write a Python program to clear the list values in a dictionary.Easy - #62Update list values
Write a Python program to add 10 to every number in the first list and subtract 25 from the second.Easy - #63Values for one key from a list of dicts
Write a Python program to extract the values for a key from a list of dictionaries.Easy - #64Length of each value
Write a Python program to map each string value to its length.Easy - #65Depth of a nested dictionary
Write a Python program to find the depth of a nested dictionary.Medium - #66Nested dictionary from a list of keys
Write a Python program to nest each item of a dictionary under a key from a list.Easy - #67Values for keys found in a list
Write a Python program to get the values of dictionary keys that are also in a list.Easy - #68Remove keys with large values
Write a Python program to remove keys whose values are greater than n.Easy - #69Remove keys whose values contain a substring
Write a Python program to remove entries whose value contains any of the given substrings.Easy - #70Print the keys
Write a Python program to print the keys of a dictionary, capitalised.Easy - #71Keep even numbers in list values
Write a Python program to keep only even numbers in each list value.Easy - #72Keys of the top 2 values
Write a Python program to find the keys of the two largest values.Easy - #73Keys with the shortest lists
Write a Python program to find the keys whose list values are the shortest.Easy - #74Frequency of values
Write a Python program to count how often each value appears in a dictionary.Easy - #75Pair keys with their single list item
Write a Python program to turn {key: [value]} into one dictionary per combination.Easy - #76Total length of string keys
Write a Python program to count the number of keys and the total characters in them.Easy - #77Group a list by a function
Write a Python program to group the elements of a list by the result of a function such aslen.Easy - #78Value at a nested path
Write a Python program to get the value at a path of keys and indexes in nested data.Medium - #79Dictionary with the most keys
Write a Python program to find which of two dictionaries has more keys.Easy - #80Unique values from list values
Write a Python program to extract the unique values from all list values, sorted.Easy - #81Which keys contain each value
Write a Python program to show, for each value, the keys whose lists contain it.Easy - #82Values of one key from many dicts
Write a Python program to get the values of a key from a list of dictionaries.Easy - #83Keys with a given value
Write a Python program to find all keys that have a given value.Easy - #84Dictionary to a list of tuples
Write a Python program to convert a dictionary to a list of (key, value) tuples.Easy - #85List of all keys
Write a Python program to get a list of all keys.Easy - #86List of all values
Write a Python program to get a list of all values.Easy - #87Initialise with default values
Write a Python program to give every name its own copy of a default settings dictionary.Easy - #88Delete several keys
Write a Python program to delete a list of keys from a dictionary.Easy - #89Rename a key
Write a Python program to rename the key “d” to “e”.Easy - #90Invert a dictionary with repeated values
Write a Python program to invert a dictionary whose values repeat, collecting keys in lists.Easy - #91Sort by the sum of list values
Write a Python program to map each key to the sum of its list and sort by that sum.Easy - #92Split a dictionary into chunks
Write a Python program to split a dictionary into dictionaries of n items.Easy - #93Reverse values and decorate keys
Write a Python program to reverse each string value and put underscores around each key.Easy - #94Sum of unique values across lists
Write a Python program to add up the distinct numbers found in all list values.Easy