A dictionary is a collection of unordered, modifiable(mutable) paired (key: value) data type.
Creating a Dictionary
To create a dictionary we use curly brackets, {} or the dict() built-in function.
# syntaxempty_dict ={}# Dictionary with data valuesdct ={'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}
Example:
person ={'first_name':'Asabeneh','last_name':'Yetayeh','age':250,'country':'Finland','is_marred':True,'skills':['JavaScript','React','Node','MongoDB','Python'],'address':{'street':'Space street','zipcode':'02210'}}
The dictionary above shows that a value could be any data types:string, boolean, list, tuple, set or a dictionary.
Dictionary Length
It checks the number of 'key: value' pairs in the dictionary.
person ={'first_name':'Asabeneh','last_name':'Yetayeh','age':250,'country':'Finland','is_marred':True,'skills':['JavaScript','React','Node','MongoDB','Python'],'address':{'street':'Space street','zipcode':'02210'}}print(len(person))# 7
Accessing Dictionary Items
We can access Dictionary items by referring to its key name.
person ={'first_name':'Asabeneh','last_name':'Yetayeh','age':250,'country':'Finland','is_marred':True,'skills':['JavaScript','React','Node','MongoDB','Python'],'address':{'street':'Space street','zipcode':'02210'}}print(person['first_name'])# Asabenehprint(person['country'])# Finlandprint(person['skills'])# ['JavaScript', 'React', 'Node', 'MongoDB', 'Python']print(person['skills'][0])# JavaScriptprint(person['address']['street'])# Space streetprint(person['city'])# Error
Accessing an item by key name raises an error if the key does not exist. To avoid this error first we have to check if a key exist or we can use the get method. The get method returns None, which is a NoneType object data type, if the key does not exist.
person ={'first_name':'Asabeneh','last_name':'Yetayeh','age':250,'country':'Finland','is_marred':True,'skills':['JavaScript','React','Node','MongoDB','Python'],'address':{'street':'Space street','zipcode':'02210'}}print(person.get('first_name'))# Asabenehprint(person.get('country'))# Finlandprint(person.get('skills'))#['HTML','CSS','JavaScript', 'React', 'Node', 'MongoDB', 'Python']print(person.get('city'))# None
Adding Items to a Dictionary
We can add new key and value pairs to a dictionary
person ={'first_name':'Asabeneh','last_name':'Yetayeh','age':250,'country':'Finland','is_marred':True,'skills':['JavaScript','React','Node','MongoDB','Python'],'address':{'street':'Space street','zipcode':'02210'}}person['job_title']='Instructor'person['skills'].append('HTML')print(person)
person ={'first_name':'Asabeneh','last_name':'Yetayeh','age':250,'country':'Finland','is_marred':True,'skills':['JavaScript','React','Node','MongoDB','Python'],'address':{'street':'Space street','zipcode':'02210'}}person['first_name']='Eyob'person['age']=252
Checking Keys in a Dictionary
We use the in operator to check if a key exist in a dictionary
pop(key): removes the item with the specified key name:
popitem(): removes the last item
del: removes an item with specified key name
# syntaxdct ={'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}dct.pop('key1')# removes key1 itemdct ={'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}dct.popitem()# removes the last itemdel dct['key2']# removes key2 item
Example:
person ={'first_name':'Asabeneh','last_name':'Yetayeh','age':250,'country':'Finland','is_marred':True,'skills':['JavaScript','React','Node','MongoDB','Python'],'address':{'street':'Space street','zipcode':'02210'}}person.pop('first_name')# Removes the firstname itemperson.popitem()# Removes the address itemdel person['is_married']# Removes the is_married item
Changing Dictionary to a List of Items
The items() method changes dictionary to a list of tuples.
import itertools# Given an array of coins and an array of quantities for each coin with the# same index, determine how many distinct sums can be made from non-zero# sets of the coins# Note: This problem took a little more working-through, with a failed brute-# force attempt that consisted of finding every combination of coins and# adding them, which failed when I needed to consider >50k coins# the overall number of coins was guaranteed to be less than about 1 million,# so the solution appeared to be a form of divide-and-conquer where each# possible sum for each coin was put into a set at that coin's index in the# original coins array, and then the sums were repeatedly combined into an# aggregate set until every coin possible coin value (given by the coins# array) had been added into the set of sums# problem considered "hard," asked by GoogledefpossibleSums(coins,quantity):# sum_map = set()# start with brute force# total_arr = [coins[i] for i, q in enumerate(quantity) for l in range(q)]# for i in range(1, len(total_arr)+1):# combos = itertools.combinations(total_arr, i)# print(combos)# for combo in combos:# sum_map.add(sum(combo))# return len(sum_map)# faster? comb_indices = [i for i inrange(len(coins))] possible_sums = []for i, c inenumerate(coins): this_set =set()for q inrange(1, 1+ quantity[i]): this_set.add(c * q) possible_sums.append(this_set)# print(possible_sums)whilelen(possible_sums)>1: possible_sums[0]=combine_sets(possible_sums[0], possible_sums[1]) possible_sums.pop(1)returnlen(possible_sums[0])defcombine_sets(set1,set2): together_set =set()for item1 in set1:for item2 in set2: together_set.add(item1 + item2) together_set.add(item1)for item2 in set2: together_set.add(item2)return together_set
# need strings[i] = strings[j] for all patterns[i] = patterns[j] to be true -# give false if strings[i] != strings[j] and patterns[i] = patterns[j] or# strings[i] = strings[j] and patterns[j] != patterns[j] - this last condition# threw me for a bit as an edge case! Need to ensure that each string is unique# to each key, not just that each key corresponds to the given string!# from a google interview set, apparentlydefareFollowingPatterns(strings,patterns): pattern_to_string ={} string_to_pattern ={}for i inrange(len(patterns)):# first, check condition that strings are equal for patterns[i]=patterns[j] this_pattern = patterns[i]if patterns[i]in pattern_to_string:if strings[i]!= pattern_to_string[this_pattern]:returnFalseelse: pattern_to_string[this_pattern]= strings[i]# now check condition that patterns are equal for strings[i]=strings[j]# if there are more keys than values, then there is not 1:1 correspondenceiflen(pattern_to_string.keys())!=len(set(pattern_to_string.values())):returnFalsereturnTrue
# gives True if two duplicate numbers in the nums array are within k distance# (inclusive) of one another, measuring by absolute difference in index# did relatively well on this one, made a greater-than/less-than flip error on# the conditional for the true case and needed to rewrite my code to remove# keys from the dictionary without editing it while looping over it, but# otherwise went well!# problem considered medium difficulty, from PalantirdefcontainsCloseNums(nums,k): num_dict ={}# setup keys for each number seen, then list their indicesfor i, item inenumerate(nums):if item in num_dict: num_dict[item].append(i)else: num_dict[item]= [i]# remove all nums that are not repeated# first make a set of keys to remove to prevent editing the dictionary size while iterating over it removals =set()for key in num_dict.keys():iflen(num_dict[key])<2: removals.add(key)# now remove each key from the num_dict that has fewer than two valuesfor key in removals: num_dict.pop(key)# now check remaining numbers to see if they fall within the desired rangefor key in num_dict.keys(): last_ind = num_dict[key][0]for next_ind in num_dict[key][1:]:if next_ind - last_ind <= k:returnTrue last_ind = next_indreturnFalse