Replace the value of a specific key in dictionary - python

I have a list of dictionaries, and I want to cycle through them and replace one of the values. The value I'm replacing is a dictionary, and I want to replace it with one of the values from the same dictionary.
Below is one of the dictionaries in the list.
{'id': '123abc',
'name': 'Metrics',
'rows': 0,
'columns': 0,
'owner': {'id': 123, 'name': 'John Doe'},
'dataCurrentAt': '2017-03-24T14:32:33Z',
'createdAt': '2017-03-24T14:32:33Z',
'pdpEnabled': False}
So I'm trying to replace the entire 'owner' value with just 'name' from the 'owner' dictionary. I hope that makes sense. Below is the section of the Python script where I have attempted to do this.
for dictionary in dataset_list:
for key, value in dictionary.items():
if key == "owner":
owner = value.get("name")
value = owner

If I understand you correctly, all what you need to do is:
dictionary["owner"] = dictionary["owner"]["name"]
This will change the value of the key "owner", to the value of the key "name".

A = {'id': '123abc', 'name': 'Metrics', 'rows': 0, 'columns': 0, 'owner': {'id': 123, 'name': 'John Doe'}, 'dataCurrentAt': '2017-03-24T14:32:33Z', 'createdAt': '2017-03-24T14:32:33Z', 'pdpEnabled': False}
A["owner"] = A["owner"]["name"]
print A
Output:
{'rows': 0, 'createdAt': '2017-03-24T14:32:33Z', 'name': 'Metrics', 'pdpEnabled': False, 'owner': 'John Doe', 'id': '123abc', 'columns': 0, 'dataCurrentAt': '2017-03-24T14:32:33Z'}

You can try:
>>> dataset_list = {'id': '123abc', 'name': 'Metrics', 'rows': 0, 'columns': 0, 'owner': {'id': 123, 'name': 'John Doe'}, 'dataCurrentAt': '2017-03-24T14:32:33Z', 'createdAt': '2017-03-24T14:32:33Z', 'pdpEnabled': False}
>>> for k,v in dataset_list.iteritems():
if k == "owner":
owner = v["name"]
dataset_list[k] = owner
>>> dataset_list
{'rows': 0, 'createdAt': '2017-03-24T14:32:33Z', 'name': 'Metrics', 'pdpEnabled': False, 'owner': 'John Doe', 'id': '123abc', 'columns': 0, 'dataCurrentAt': '2017-03-24T14:32:33Z'}

for dictionary in dataset_list:
for key, value in dictionary.items():
if key == "owner":
dictionary["owner"] = dictionary["owner"]["name"]

Related

Remove nested element occurs twice but should be only once

I have a problem. I want to remove all nested elements inside a dict. But unfortunately my code does not work. Every nested element occurs twice, but it should be occurs only once.
What is the problem for that?
Method
def nested_dict(dictionaries):
my_list = []
for my_Dict in dictionaries:
my_new_dict = {}
for key in my_Dict.keys():
if isinstance(my_Dict[key], dict):
idx = str(uuid.uuid4())
my_Dict[key]["__id"] = idx
my_new_dict[key] = my_Dict[key]
my_Dict[key] = idx
my_list.append(my_new_dict)
return my_list
Running example
import uuid
my_Dict = {
'_key': '1',
'group': 'test',
'data': {},
'type': '',
'code': '007',
'conType': '1',
'flag': None,
'createdAt': '2021',
'currency': 'EUR',
'detail': {
'selector': {
'number': '12312',
'isTrue': True,
'requirements': [{
'type': 'customer',
'requirement': '1'}]
}
}
}
my_Dict2 = {
'_key': '2',
'group': 'test',
'data2': {},
'type': '',
'code': '007',
'conType': '1',
'flag': None,
'createdAt': '2021',
'currency': 'EUR',
'detail2': {
'selector': {
'number': '12312',
'isTrue': True,
'requirements': [{
'type': 'customer',
'requirement': '1'}]
}
}
}
dictionaries = [my_Dict, my_Dict2]
def nested_dict(dictionaries):
my_list = []
for my_Dict in dictionaries:
my_new_dict = {}
for key in my_Dict.keys():
if isinstance(my_Dict[key], dict):
idx = str(uuid.uuid4())
my_Dict[key]["__id"] = idx
my_new_dict[key] = my_Dict[key]
my_Dict[key] = idx
my_list.append(my_new_dict)
return my_list
result = nested_dict(dictionaries)
result
[OUT]
[{'data': {'__id': '46f4eb3d-977c-4da4-a99c-c9bfa831b96e'},
'detail': {'selector': {'number': '12312',
'isTrue': True,
'requirements': [{'type': 'customer', 'requirement': '1'}]},
'__id': 'fad4053e-75e5-4a03-93b6-67e0df814d23'}},
{'data': {'__id': '46f4eb3d-977c-4da4-a99c-c9bfa831b96e'},
'detail': {'selector': {'number': '12312',
'isTrue': True,
'requirements': [{'type': 'customer', 'requirement': '1'}]},
'__id': 'fad4053e-75e5-4a03-93b6-67e0df814d23'}},
{'data2': {'__id': '6afcf48e-508c-476b-98f3-9bf1e8370fb4'},
'detail2': {'selector': {'number': '12312',
'isTrue': True,
'requirements': [{'type': 'customer', 'requirement': '1'}]},
'__id': '2d4745ea-decd-45dc-aa0b-7bea5c449c34'}},
{'data2': {'__id': '6afcf48e-508c-476b-98f3-9bf1e8370fb4'},
'detail2': {'selector': {'number': '12312',
'isTrue': True,
'requirements': [{'type': 'customer', 'requirement': '1'}]},
'__id': '2d4745ea-decd-45dc-aa0b-7bea5c449c34'}}]
What I want
[{'data': {'__id': '46f4eb3d-977c-4da4-a99c-c9bfa831b96e'},
'detail': {'selector': {'number': '12312',
'isTrue': True,
'requirements': [{'type': 'customer', 'requirement': '1'}]},
'__id': 'fad4053e-75e5-4a03-93b6-67e0df814d23'}},
{'data2': {'__id': '6afcf48e-508c-476b-98f3-9bf1e8370fb4'},
'detail2': {'selector': {'number': '12312',
'isTrue': True,
'requirements': [{'type': 'customer', 'requirement': '1'}]},
'__id': '2d4745ea-decd-45dc-aa0b-7bea5c449c34'}}]
import uuid
import json
my_Dict = {
'_key': '1',
'group': 'test',
'data': {},
'type': '',
'code': '007',
'conType': '1',
'flag': None,
'createdAt': '2021',
'currency': 'EUR',
'detail': {
'selector': {
'number': '12312',
'isTrue': True,
'requirements': [{
'type': 'customer',
'requirement': '1'}]
}
}
}
my_Dict2 = {
'_key': '2',
'group': 'test',
'data2': {},
'type': '',
'code': '007',
'conType': '1',
'flag': None,
'createdAt': '2021',
'currency': 'EUR',
'detail2': {
'selector': {
'number': '12312',
'isTrue': True,
'requirements': [{
'type': 'customer',
'requirement': '1'}]
}
}
}
dictionaries = [my_Dict, my_Dict2]
def nested_dict(dictionaries):
my_list = []
for my_Dict in dictionaries:
my_new_dict = {}
for key in my_Dict.keys():
if isinstance(my_Dict[key], dict):
idx = str(uuid.uuid4())
my_Dict[key]["__id"] = idx
my_new_dict[key] = my_Dict[key]
my_Dict[key] = idx
my_list.append(my_new_dict)
return my_list
output:
[
{
"data": {
"__id": "5c6769cf-01e5-4f5d-acfa-622472163aba"
},
"detail": {
"selector": {
"number": "12312",
"isTrue": true,
"requirements": [
{
"type": "customer",
"requirement": "1"
}
]
},
"__id": "d167277f-4d02-4d53-934b-131187f6f214"
}
},
{
"data2": {
"__id": "e9182913-c2fc-4d60-adb8-b0b8274faf50"
},
"detail2": {
"selector": {
"number": "12312",
"isTrue": true,
"requirements": [
{
"type": "customer",
"requirement": "1"
}
]
},
"__id": "46e6be7b-8903-4d2a-a768-f6b24fcc5d31"
}
}
]
only minor changes needed that is you are appending the list within inner for loop but you should do it at outer for loop level. I have pasted the code with output which I got
I think it is because my_new_dict is holding an object that is changed by the time it appends to the list.
def nested_dict(dictionaries):
my_list = []
for my_Dict in dictionaries:
my_new_dict = {}
for key in my_Dict.keys():
if isinstance(my_Dict[key], dict):
idx = str(uuid.uuid4())
my_Dict[key]["__id"] = idx
my_new_dict[key] = my_Dict[key]
my_Dict[key] = idx
my_list.append({key: my_new_dict[key]})
print(my_list)
return my_list

Populate Python dictionary with value in nested dictionary

I am using the AccuWeather RESTFul API to get the current weather conditions in the top 50 cities. One object of the JSON response looks like this:
{'Key': '28143', 'LocalizedName': 'Dhaka', 'EnglishName': 'Dhaka', 'Country': {'ID': 'BD', 'LocalizedName': 'Bangladesh', 'EnglishName': 'Bangladesh'}, 'TimeZone': {'Code': 'BDT', 'Name': 'Asia/Dhaka', 'GmtOffset': 6.0, 'IsDaylightSaving': False, 'NextOffsetChange': None}, 'GeoPosition': {'Latitude': 23.7098, 'Longitude': 90.40711, 'Elevation': {'Metric': {'Value': 5.0, 'Unit': 'm', 'UnitType': 5}, 'Imperial': {'Value': 16.0, 'Unit': 'ft', 'UnitType': 0}}}, 'LocalObservationDateTime': '2021-10-09T13:11:00+06:00', 'EpochTime': 1633763460, 'WeatherText': 'Mostly cloudy', 'WeatherIcon': 6, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 32.2, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 90.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us', 'Link': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us'}
Now I want to populate a dictionary with 1) "EnglishName", 2) "WeatherText", and 3) "Temperature (Celsius)".
I do manage to get a key-value pair with "EnglishName" and "WeatherText" as below:
weatherResponse = result.json()
mydictionary = dict()
for p in weatherResponse:
print(p["EnglishName"])
print(p["LocalObservationDateTime"])
print(p["WeatherText"])
temp_C = list(p["Temperature"]["Metric"].values())[0]
print(f"Temperature in Celsius: {temp_C}")
print("--------")
mydictionary[p["EnglishName"]] = p["WeatherText"]
How can I assign the "temp_C" value of each key to the dictionary as well?
I tried the append function but that does not work.
Any help is appreciated!
Instead of adding only one value p["WeatherText"] to your dictionary you can add multiple through use of tuples, like (a,b). Please see the below line.
mydictionary[p["EnglishName"]] = (p["WeatherText"], p["Temperature"]["Metric"]["Value"])
This above line you can use to assign to your dictionary key multiple values, sample output from this:
{'Dhaka': ('Mostly cloudy', 32.2)}
You can read tuples just like lists
mydictionary["Dhaka"][0] # This for getting the text
mydictionary["Dhaka"][1] # This for getting the value
Also tuples may look similar to lists but in this case it is recommended to use tuples because lists should store same data type values and tuples can store multiple datatype values.
I want to populate a dictionary with 1) "EnglishName", 2) "WeatherText", and 3) "Temperature (Celsius)".
See below
data = [{
'Key': '28143',
'LocalizedName': 'Dhaka',
'EnglishName': 'Dhaka',
'Country': {
'ID': 'BD',
'LocalizedName': 'Bangladesh',
'EnglishName': 'Bangladesh'
},
'TimeZone': {
'Code': 'BDT',
'Name': 'Asia/Dhaka',
'GmtOffset': 6.0,
'IsDaylightSaving': False,
'NextOffsetChange': None
},
'GeoPosition': {
'Latitude': 23.7098,
'Longitude': 90.40711,
'Elevation': {
'Metric': {
'Value': 5.0,
'Unit': 'm',
'UnitType': 5
},
'Imperial': {
'Value': 16.0,
'Unit': 'ft',
'UnitType': 0
}
}
},
'LocalObservationDateTime': '2021-10-09T13:11:00+06:00',
'EpochTime': 1633763460,
'WeatherText': 'Mostly cloudy',
'WeatherIcon': 6,
'HasPrecipitation': False,
'PrecipitationType': None,
'IsDayTime': True,
'Temperature': {
'Metric': {
'Value': 32.2,
'Unit': 'C',
'UnitType': 17
},
'Imperial': {
'Value': 90.0,
'Unit': 'F',
'UnitType': 18
}
},
'MobileLink': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us',
'Link': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us'
}]
filtered_data = [{'EnglishName':e.get('EnglishName','NA'),'WeatherText':e.get('WeatherText','NA'),'temp_C':e.get('Temperature').get('Metric').get('Value')} for e in data]
print(filtered_data)
output
[{'EnglishName': 'Dhaka', 'WeatherText': 'Mostly cloudy', 'temp_C': 32.2}]

Extract values from dicts inside lists

I'm trying to extract the values from this JSON file, but I having some trouble to extract the data inside from lists in the dict values. For example, in the city and state, I would like to get only the name values and create a Pandas Dataframe and select only some keys like this.
I tried using some for with get methods techniques, but without success.
{'birthday': ['1987-07-13T00:00:00.000Z'],
'cpf': ['9999999999999'],
'rg': [],
'gender': ['Feminino'],
'email': ['my_user#bol.com.br'],
'phone_numbers': ['51999999999'],
'photo': [],
'id': 11111111,
'duplicate_id': -1,
'name': 'My User',
'cnpj': [],
'company_name': '[]',
'city': [{'id': 0001, 'name': 'Porto Alegre'}],
'state': [{'id': 100, 'name': 'Rio Grande do Sul', 'fs': 'RS'}],
'type': 'Private Person',
'tags': [],
'pending_tickets_count': 0}
In [123]: data
Out[123]:
{'birthday': ['1987-07-13T00:00:00.000Z'],
'cpf': ['9999999999999'],
'rg': [],
'gender': ['Feminino'],
'email': ['my_user#bol.com.br'],
'phone_numbers': ['51999999999'],
'photo': [],
'id': 11111111,
'duplicate_id': -1,
'name': 'My User',
'cnpj': [],
'company_name': '[]',
'city': [{'id': '0001', 'name': 'Porto Alegre'}],
'state': [{'id': 100, 'name': 'Rio Grande do Sul', 'fs': 'RS'}],
'type': 'Private Person',
'tags': [],
'pending_tickets_count': 0}
In [124]: data2 = {k:v for k,v in data.items() if k in required}
In [125]: data2
Out[125]:
{'birthday': ['1987-07-13T00:00:00.000Z'],
'gender': ['Feminino'],
'id': 11111111,
'name': 'My User',
'city': [{'id': '0001', 'name': 'Porto Alegre'}],
'state': [{'id': 100, 'name': 'Rio Grande do Sul', 'fs': 'RS'}]}
In [126]: pd.DataFrame(data2).assign(
...: city_name=lambda x: x['city'].str.get('name'),
...: state_name=lambda x: x['state'].str.get('name'),
...: state_fs=lambda x: x['state'].str.get('fs')
...: ).drop(['state', 'city'], axis=1)
Out[126]:
birthday gender id name city_name state_name state_fs
0 1987-07-13T00:00:00.000Z Feminino 11111111 My User Porto Alegre Rio Grande do Sul RS
reason why data2 is required is that you can't have columns that differ in length. So in this case, pd.DataFrame(data) won't work as rg has 0 items but birthday has 1 item.
Also something to look at if you are directly dealing with json files is pd.json_normalize

Generate Dictionary from nested List, Python 3.6

I have below List:
dimensionList = [{'key': 2109290, 'id': 'R', 'name': 'Reporter', 'isGeo': True, 'geoType': 'region'},
{'key': 2109300, 'id': 'C', 'name': 'Commodity', 'isGeo': False, 'geoType': None},
{'key': 2109310, 'id': 'P', 'name': 'Partner', 'isGeo': True, 'geoType': 'region'},
{'key': 2109320, 'id': 'TF', 'name': 'Trade Flow', 'isGeo': False, 'geoType': None},
{'key': 2109330, 'id': 'I', 'name': 'Measure', 'isGeo': False, 'geoType': None}]
I want to create dictionary from this list
Need Values of 'id' as Id of dictionary & 'name' as Values of dictionary
Expected Results:-
ResultsDict = {'R':'Reporter', 'C':'Commodity', 'P':'Partner', 'TF':'Trade Flow', 'I':'Measure'}
Use dict comprehension:
d = {x['id']:x['name'] for x in dimensionList}
print (d)
{'R': 'Reporter', 'C': 'Commodity', 'P': 'Partner', 'TF': 'Trade Flow', 'I': 'Measure'}
You need to loop through the list of dictionaries, pulling out the bits you want and adding them to your new dictionary.
ResultsDict = {}
for dict_item in dimensionList:
id = dict_item ['id']
name = dict_item ['name']
ResultsDict[id] = name
print(ResultsDict)

Find the dictionary from List which has key-pair 'isGeo':True

How to Find the dictionary from List which has key-pair 'isGeo':True
dimensions = [{'key': 2600330, 'id': 'location', 'name': 'Location', 'isGeo': True, 'geoType': 'region'}, {'key': 2600340, 'id': 'subject', 'name': 'Subject', 'isGeo': False, 'geoType': None}, {'key': 2600350, 'id': 'measure', 'name': 'Measure', 'isGeo': False, 'geoType': None}]
I want to below result:
{'key': 2600330, 'id': 'location', 'name': 'Location', 'isGeo': True, 'geoType': 'region'}
Use next with a generator expression:
res = next((d for d in dimensions if d['isGeo']), None)
{'key': 2600330, 'id': 'location', 'name': 'Location', 'isGeo': True, 'geoType': 'region'}
Since you tagged pandas, you can also use Pandas:
import pandas as pd
df = pd.DataFrame(dimensions)
res = df.loc[df['isGeo']].iloc[0].to_dict()
The above solutions assume you want only the first dictionary satisfying your condition. If you want a list of dictionaries use:
res = [d for d in dimensions if d['isGeo']]
res = df.loc[df['isGeo']].to_dict('records')
Another way is list comprehension
dimensions = [{'key': 2600330, 'id': 'location', 'name': 'Location', 'isGeo': True, 'geoType': 'region'}, {'key': 2600340, 'id': 'subject', 'name': 'Subject', 'isGeo': False, 'geoType': None}, {'key': 2600350, 'id': 'measure', 'name': 'Measure', 'isGeo': False, 'geoType': None}]
geoDimension = [dimension for dimension in dimensions if dimension['isGeo']][0]
Output : {'key': 2600330, 'id': 'location', 'name': 'Location', 'isGeo': True, 'geoType': 'region'}

Categories