Different ways to create dictionary in python

Advertisements I was exploring different ways to create a dictionary in Python 3 (3.11.1). ChatGPT suggested that I can create a dictionary by using the following syntax: my_dict = dict(‘key1’=’value1’, ‘key2’=’value2’, ‘key3’=’value3’) I tried this in IDLE, but I am getting SyntaxError: expression cannot contain assignment, perhaps you meant "=="?. To confirm if ChatGPT may… Read More Different ways to create dictionary in python

How to check if two key value pairs exists in list of Dictionary in Python

Advertisements I’m trying to check if a combination of two key-value pairs exists in list of dictionaries. Here is my list. data = [ {‘transaction’:’abc123′, ‘stage_one’:’Started’, ‘stage_two’:’Complete’, ‘version’: 12), {‘transaction’:’abc123′, ‘stage_one’:’Started’, ‘stage_two’:’started’, ‘version’: 12), {‘transaction’:’abc123′, ‘stage_one’:’Started’, ‘stage_two’:’processing’, ‘version’: 12), {‘transaction’:’abc123′, ‘stage_one’:’Started’, ‘stage_two’:’Complete’, ‘version’: 12) ] I have tried the below line of code, but I… Read More How to check if two key value pairs exists in list of Dictionary in Python

in the below program, i don't understand how was the data stored from the list into the count_dict

Advertisements sample_list = [11, 45, 8, 11, 23, 45, 23, 45, 89] print("Original list ", sample_list) count_dict = dict() for item in sample_list: if item in count_dict: count_dict[item] += 1 # how was the data stored into the variable count_dict? else: count_dict[item] = 1 print("Printing count of each item ", count_dict) i just don’t understand… Read More in the below program, i don't understand how was the data stored from the list into the count_dict

Python: If dataframe column matches dictionary key then return boolean for new column

Advertisements I’m having difficulties trying to get this to work. So to summarize: I have a dataframe, I have a dictionary with keys of certain values in a certain dataframe value, and I want to create a new dataframe column of true/false if they match or not. So here is an example of the data:… Read More Python: If dataframe column matches dictionary key then return boolean for new column

Convert list of dictionaries to dictionary of dictionaries

Advertisements I have a list of dictionaries ls1 = [{‘AccountBalance’: ’78’, ‘Status’: ‘0’}, {‘AccountBalance’: ’56’, ‘Status’: ‘1’}, {‘AccountBalance’: ’34’, ‘Status’: ‘0’}, {‘AccountBalance’: ’12’, ‘Status’: ‘0’}] I would like to convert this to a dictionary of 2 dictionaries dict1 = {‘AccountBalance’: {0: ’78’, 1: ’56’, 2: ’34’, 3: ’12’}, ‘Status’: {0: ‘0’, 1: ‘1’, 2: ‘0’,… Read More Convert list of dictionaries to dictionary of dictionaries

Sort second dictionary based on value sequence of first

Advertisements I am trying to sort the dictionary based on the value of the first dictionary sequence Example: s = {"J":1, "R":2, "E":3, "L":4] o = {"J":"V140", "E":"21:45", "R":"SUN MON TUE WED THU", "L":"V2730"] expecting the o dictionary in the key order of s o = {"J":"V140","R":"SUN MON TUE WED THU", "E":"21:45", "L":"V2730"} I looked… Read More Sort second dictionary based on value sequence of first