Printing nested dictionary

I have 2 dictionaries: movie_collections_dict = {‘Avatar’:0, ‘Titanic’:0, ‘StarWar’:0} #which indicates the revenues of the movies and {‘avatar’: {‘adult’: 0, ‘child’: 0, ‘concession’: 0, ‘senior’: 0, ‘student’: 0}, ‘starwar’: {‘adult’: 0, ‘child’: 0, ‘concession’: 0, ‘senior’: 0, ‘student’: 0}, ‘titanic’: {‘adult’: 0, ‘child’: 0, ‘concession’: 0, ‘senior’: 0, ‘student’: 0}} #which shows that this much… Read More Printing nested dictionary

Python nested dictionary – remove "" and data with extra spaces but keep None values

I have a dictionary and would like to keep None values but remove values with "" and also values of any combination of " "’s I have the following dictionary: {‘UserName’: ”, ‘Location’: [{‘City’: ”, ‘Country’: ‘Japan’, ‘Address 1’: ‘ ‘, ‘Address 2’: ‘ ‘}], ‘PhoneNumber’: [{‘Number’: ‘123-456-7890’, ‘ContactTimes’: ”, ‘PreferredLanguage’: None}], ‘EmailAddress’: [{‘Email’: ‘test@test.com’,… Read More Python nested dictionary – remove "" and data with extra spaces but keep None values

VBA Dictionary duplicating entries when using CStr for keys

While trying to fix a broken code with arrays and dictionaries, I came upon something weird when filling the dictionary (I have since used a different workaround) but was wondering what’s causing the "duplication" of keys in the dictionary: Current Workaround: Dim lvbData() lvbData = wsHeadH.Range("A1:C" & lRowHeadH).Value ‘to build the keys lRow = wsData.Range("B"… Read More VBA Dictionary duplicating entries when using CStr for keys

How to use dictionary comprehension to combine two lists into a nested dictionary?

I want to combine the following lists into a nested dictionary using list comprehension. dates = [‘2023-04-01’, ‘2023-04-07’, ‘2023-04-17’, ‘2023-04-19’, ‘2023-04-25’] events_name = [ ‘PyTexas’, ‘PyCamp Spain’, ‘PyData Berlin’, ‘PyCon US’, ‘PyLadies Amsterdam’] The dictionary should look like this: python_events = { 0: {‘time’: ‘2023-04-01’, ‘name’: ‘PyTexas’}, 1: {‘time’: ‘2023-04-07’, ‘name’: ‘PyCamp Spain’}, 2: {‘time’:… Read More How to use dictionary comprehension to combine two lists into a nested dictionary?

How to change all the keys of a dictionary?

I have a dictionary like this {‘lastname’:’John’, ‘fistname’:’Doe’, ‘id’:’xxxxx’} and I would like to add a prefix to all key values. The outcome should look like {‘contact_lastname’:’John’, ‘contact_fistname’:’Doe’, ‘contact_id’:’xxxxx’} I tried to achieve this by a lambda function, but did not work. original = {‘lastname’:’John’, ‘fistname’:’Doe’, ‘id’:’xxxxx’} modified = {(lambda k: ‘contact_’+k) :v for k,v… Read More How to change all the keys of a dictionary?

Create dictionary with pairs from column from pandas dataframe using regex

I have the following dataframe import pandas as pd df = pd.DataFrame({‘Original’: [92,93,94,95,100,101,102], ‘Sub_90’: [99,98,99,100,102,101,np.nan], ‘Sub_80’: [99,98,99,100,102,np.nan,np.nan], ‘Gen_90’: [99,98,99,100,102,101,101], ‘Gen_80’: [99,98,99,100,102,101,100]}) I would like to create the following dictionary { ‘Gen_90’: ‘Original’, ‘Sub_90’: ‘Gen_90’, ‘Gen_80’: ‘Original’, ‘Sub_80’: ‘Gen_80’, } using regex (because at my original data I also have Gen_70, Gen_60, … , Gen_10 and… Read More Create dictionary with pairs from column from pandas dataframe using regex

How to filter out dictionaries with the same value for a certain key from a list?

I have the following list of objects: l = [{‘name’: ‘Mike’, ‘age’: 31}, {‘name’: ‘Peter’, ‘age’: 29}, {‘name’: ‘Mike’, ‘age’: 44}] I want to filter it based on the name, and because "Mike" is a duplicate in this case, I want to remove all entries with name=Mike (regardless of the age). (i.e. to get the… Read More How to filter out dictionaries with the same value for a certain key from a list?