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

Advertisements 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:… Read More How to use dictionary comprehension to combine two lists into a nested dictionary?

How to change all the keys of a dictionary?

Advertisements 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… Read More How to change all the keys of a dictionary?

Create dictionary with pairs from column from pandas dataframe using regex

Advertisements 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… 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?

Advertisements 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… Read More How to filter out dictionaries with the same value for a certain key from a list?

Generate list of values from nested dictionary as values

Advertisements I have a nested dictionary like this: d = {‘A’: {‘A’: 0.11, ‘C’: 0.12, ‘D’: 1.0}, ‘B’: {‘B’: 0.13, ‘C’: 0.14}} I want to generate this output where in values only the list of keys of inner dictionary are selected. output dictionary output = {‘A’:[‘A’, ‘C’, ‘D’], ‘B’:[‘B’, ‘C’]} Is there any way to… Read More Generate list of values from nested dictionary as values

Pandas DataFrame Created from Dictionary vs Created from List

Advertisements Is there a line or two of code that would make the DataFrame created from lists behave like the one created from a dictionary? #DataFrame created from dictionary, this works: import pandas as pd data= {‘Salary’: [30000, 40000, 50000, 85000, 75000], ‘Exp’: [1, 3, 5, 10, 25], ‘Gender’: [‘M’,’F’, ‘M’, ‘F’, ‘M’]} df =… Read More Pandas DataFrame Created from Dictionary vs Created from List

Iterate over a list of dictionaries and get value from previous dictionary

Advertisements I have a list of dictionaries: history_changes = [ {‘userName’: ‘recg’, ‘modificationDate’: ‘2022-07-14T04:01:39+00:00’, ‘changeComment’: ‘more details about L2’}, {‘userName’: ‘artf’, ‘modificationDate’: ‘2022-07-15T04:01:39+00:00’, ‘changeComment’: ‘more details about L1’}, {‘username’: ‘zrt’, ‘modificationDate’: ‘2022-07-16T04:01:39+00:00’, ‘changeComment’: ‘more details about L3′}] I’m iterating over it and trying to insert data in a database. for record in history_changes[1:]: comment =… Read More Iterate over a list of dictionaries and get value from previous dictionary