Append dict to list of dict - python

I'm getting members from a Google group using the directory API. This is what is returned:
{'etag': '"..."',
'kind': 'admin#directory#members',
'members': [{'email': 'a#a.com',
'etag': '"..."',
'id': '....',
'kind': 'admin#directory#member',
'role': 'MEMBER',
'type': 'USER'},
{'email': 'b#a.com',
'etag': '"..."',
'id': '...',
'kind': 'admin#directory#member',
'role': 'MEMBER',
'type': 'USER'},
...],
'nextPageToken': '...'}
There are multiple pages being returned and I want to append the next set of members to the members key of this dictionary...I think.
Here is my code so far:
for group in groups_list:
members_service = service.members()
member_request = members_service.list(groupKey=group['email'], maxResults=200, roles='MEMBER')
memberEmails = {}
while member_request is not None:
member_results = member_request.execute()
memberEmails.update(member_results)
member_request = members_service.list_next(member_request, member_results)
I don't think memberEmails.update(member_results) will work here because it overwrites and updates existing keys.
How can I accomplish this? or what are some alternatives?

I wonder if you want to treat memberEmails like a list and extend it with the value associated with the members key in the result. For example:
for group in groups_list:
members_service = service.members()
member_request = members_service.list(groupKey=group['email'], maxResults=200, roles='MEMBER')
member_emails = list()
while member_request is not None:
member_results = member_request.execute()
member_emails.extend(member_request['members'])
member_request = members_service.list_next(member_request, member_results)

Related

How to add list into api call last array?

I have this code the call an api and get an answer.
I need to perform a call for ever issue_id, and store the issue id with the correct response:
issues_ids = [10495]
def get_changelog(issue_id: int):
url = f'{base_url}/{issue_id}/changelog'
response = requests.request("GET",url,headers=headers,auth=auth)
return (response.json())
def parse_json(response):
keylsit = []
for item in response['values']:
key = {
'id': item['id'],
'items': item['items']
}
(keylsit.append(key))
return keylsit
mainlist = []
for i in issues_ids:
print(i)
mainlist.extend(parse_json(get_changelog(i)))
print(mainlist)
current print :
10495
[{'id': '13613', 'items': [{'field': 'Organization text', 'fieldtype': 'custom', 'fieldId': 'customfield_10039', 'from': None, 'fromString': None, 'to': None, 'toString': 'Jabil'}]}]
I need to add the 10495 with in this array as a new key
[{issue_id: 10495, 'id': '13613', 'items': [{'field': 'Organization text', 'fieldtype': 'custom', 'fieldId': 'customfield_10039', 'from': None, 'fromString': None, 'to': None, 'toString': 'Jabil'}]}]
I tried different methods such as insert, append...

Python: when trying to extract certain keys, how can I avoid a KeyError when in some dict elements, the key value is missing from APi json?

I can successfully extract every column using Python, except the one I need most (order_id) from an API generated json that lists field reps interactions with clients.
Not all interactions result in orders; there are multiple types of interactions. I know I will need to add the flag to show 'None' and then in my for loop and an if-statement to check whether the order_id is null or not. If not 'None/null', add it to the list.
I just cannot figure it out so would appreciate every bit of help!
This is the code that works:
import requests
import json
r = requests.get(baseurl + endpoint + '?page_number=1' + '&page_size=2', headers=headers)
output = r.json()
interactions_list = []
for item in output['data']:
columns = {
'id': item['id'],
'number': item['user_id'],
'name': item['user_name'],
}
interactions_list.append(columns)
print(interactions_list)
This returns an error-free result:
[{'id': 1, 'number': 6, 'name': 'Johnny'}, {'id': 2, 'number': 7, 'name': 'David'}]
When I include the order_id in the loop:
interactions_list = []
for item in output['data']:
columns = {
'id': item['id'],
'number': item['user_id'],
'name': item['user_name'],
'order': item['order_id'],
}
interactions_list.append(columns)
It returns:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17856/1993147086.py in <module>
6 'number': item['user_id'],
7 'name': item['user_name'],
----> 8 'order': item['order_id'],
9 }
10
KeyError: 'order_id'
Use the get method of the dictionary:
columns = {
'id': item.get('id'),
'number': item.get('user_id'),
'name': item.get('user_name'),
'order': item.get('order_id'),
}
This will set your missing values to None. If you want to choose what the None value is, pass a second argument to get e.g. item.get('user_name', 'N/A')
EDIT: To conditionally add items based on the presence of the order_id
interactions_list = []
for item in output['data']:
if 'order_id' in item:
columns = {
'id': item.get('id'),
'number': item.get('user_id'),
'name': item.get('user_name', 'N/A'),
'order': item.get('order_id'),
}
interactions_list.append(columns)
Alternatively, you can use a list comprehension approach, which should be slightly more efficient than using list.append in a loop:
output = {'data': [{'order_id': 'n/a', 'id': '123'}]}
interactions_list = [
{
'id': item.get('id'),
'number': item.get('user_id'),
'name': item.get('user_name', 'N/A'),
'order': item.get('order_id'),
} for item in output['data'] if 'order_id' in item
]
# [{'id': '123', 'number': None, 'name': 'N/A', 'order': 'n/a'}]

Check if list as value(with atleast one element) exists in dictionary and return it

I have a following dictionary:
dict1 = {'Diacto Group': {'id': 7547,
'type': 'user',
'name': 'Diacto Group',
'filters': [{'column': 'Core Area',
'values': ['Missiles & Aviation Technology'],
'operator': 'EQUALS',
'not': False}],
'users': [],
'virtualUsers': [],
'groups': [360305499]},
'Diacto People': {'id': 7548,
'type': 'user',
'name': 'Diacto People',
'filters': [{'column': 'Core Area',
'values': ['Aircraft Company', 'Aviation Technology'],
'operator': 'EQUALS',
'not': False}],
'users': [326197441, 1293859642],
'virtualUsers': [],
'groups': []},
}
Basically I want to extract either one of the lists from 'users' or 'groups' if they have list containing atleast one value. I want the final output to look like this:
l1 = [# Extracted list as value from 'group' key from Diacto Group key as users key was blank
# list.
[360305499],
# Extracted list as value from 'users' key from Diacto People key as groups key was
# blank list.
[326197441, 1293859642]
]
List comprehension would be more preferable if possible.
Thank you for the efforts and time you put into this.
The simplest I can think of with comprehension, provided either 'users' or 'groups' is not empty:
[v['users']+v['groups'] for v in dict1.values()]
I think it would be possible to do with list comprehension, but extremely difficult to understand.
This would be my approach:
l1 = []
for k in dict1:
if len(dict1[k]['users']) > 0:
l1.append(dict1[k]['users'])
if len(dict1[k]['groups']) > 0:
l1.append(dict1[k]['groups'])
print(l1)
Using list comprehension and filtering out cases where both "users" and "groups" are empty:
l1 = [v["users"]+v["groups"] for _, v in dict1.items() if v["users"]+v["groups"]]

How can I print a list without the first square brackets?

How can I print a list without the first square brackets?
[[{'id': '5b0cecebc15ae360393438c2', 'owner': {'id': '58ebd0b12c593e5253505ad4'}, 'linked_id': None, 'type': 'product', 'name': 'Трансформаторное масло Т-1500у'}]]
At the same time, it is necessary to preserve its essence of the list.
So that the parser can read the id.
items = response['data']
usr_ids = items[i]["owner"]["id"]
Simply print the first item of the list
lst = [[{'id': '5b0cecebc15ae360393438c2', 'owner': {'id': '58ebd0b12c593e5253505ad4'}, 'linked_id': None, 'type': 'product', 'name': 'Трансформаторное масло Т-1500у'}]]
print(lst[0])

Extract specific keys from list of dict in python. Sentinelhub

I seem to be stuck on very simple task. I'm still dipping my toes into Python.
I'm trying to download Sentinel 2 Images with SentinelHub API:SentinelHub
The result of data that my code returns is like this:
{'geometry': {'coordinates': [[[[35.895906644, 31.602691754],
[36.264307655, 31.593801516],
[36.230618703, 30.604681346],
[35.642363693, 30.617971909],
[35.678587829, 30.757888786],
[35.715700562, 30.905919341],
[35.754290061, 31.053632806],
[35.793289298, 31.206946419],
[35.895906644, 31.602691754]]]],
'type': 'MultiPolygon'},
'id': 'ee923fac-0097-58a8-b861-b07d89b99310',
'properties': {'**productType**': '**S2MSI1C**',
'centroid': {'coordinates': [18.1321538275, 31.10368655], 'type': 'Point'},
'cloudCover': 10.68,
'collection': 'Sentinel2',
'completionDate': '2017-06-07T08:15:54Z',
'description': None,
'instrument': 'MSI',
'keywords': [],
'license': {'description': {'shortName': 'No license'},
'grantedCountries': None,
'grantedFlags': None,
'grantedOrganizationCountries': None,
'hasToBeSigned': 'never',
'licenseId': 'unlicensed',
'signatureQuota': -1,
'viewService': 'public'},
'links': [{'href': 'http://opensearch.sentinel-hub.com/resto/collections/Sentinel2/ee923fac-0097-58a8-b861-b07d89b99310.json?&lang=en',
'rel': 'self',
'title': 'GeoJSON link for ee923fac-0097-58a8-b861-b07d89b99310',
'type': 'application/json'}],
'orbitNumber': 10228,
'organisationName': None,
'parentIdentifier': None,
'platform': 'Sentinel-2',
'processingLevel': '1C',
'productIdentifier': 'S2A_OPER_MSI_L1C_TL_SGS__20170607T120016_A010228_T36RYV_N02.05',
'published': '2017-07-26T13:09:17.405352Z',
'quicklook': None,
'resolution': 10,
's3Path': 'tiles/36/R/YV/2017/6/7/0',
's3URI': 's3://sentinel-s2-l1c/tiles/36/R/YV/2017/6/7/0/',
'sensorMode': None,
'services': {'download': {'mimeType': 'text/html',
'url': 'http://sentinel-s2-l1c.s3-website.eu-central-1.amazonaws.com#tiles/36/R/YV/2017/6/7/0/'}},
'sgsId': 2168915,
'snowCover': 0,
'spacecraft': 'S2A',
'startDate': '2017-06-07T08:15:54Z',
'thumbnail': None,
'title': 'S2A_OPER_MSI_L1C_TL_SGS__20170607T120016_A010228_T36RYV_N02.05',
'updated': '2017-07-26T13:09:17.405352Z'},
'type': 'Feature'}
Can you explain how can I iterate through this set of data and extract only 'productType'? For example, if there are several similar data sets it would return only different product types.
My code is :
import matplotlib.pyplot as plt
import numpy as np
from sentinelhub import AwsProductRequest, AwsTileRequest, AwsTile, BBox, CRS
betsiboka_coords_wgs84 = [31.245117,33.897777,34.936523,36.129002]
bbox = BBox(bbox=betsiboka_coords_wgs84, crs=CRS.WGS84)
date= '2017-06-05',('2017-06-08')
data=sentinelhub.opensearch.get_area_info(bbox, date_interval=date, maxcc=None)
for i in data:
print(i)
Based on what you have provided, replace your bottom for loop:
for i in data:
print(i)
with the following:
for i in data:
print(i['properties']['**productType**'])
If you want to access only the propertyType you can use i['properties']['productType'] in your for loop. If you want to access it any time you want without writing each time those keys, you can define a generator like this:
def property_types(data_array):
for data in data_array
yield data['properties']['propertyType']
So you can use it like this in a loop (your data_array is data, as returned by sentinelhub api):
for property_type in property_types(data):
# do stuff with property_type
keys = []
for key in d.keys():
if key == 'properties':
for k in d[key].keys():
if k == '**productType**' and k not in keys:
keys.append(d[key][k])
print(keys)
Getting only specific (nested) values: Since your request key is nested, and resides inside the parent "properties" object, you need to access it first, preferably using the get method. This can be done as follows (note the '{}' parameter in the first get, this returns an empty dictionary if the first key is not present)
data_dictionary = json.loads(data_string)
product_type = data_dictionary.get('properties', {}).get('**productType**')
You can then aggregate the different product_type objects in a set, which will automatically guarantee that no 2 objects are the same
product_type_set = set()
product_type.add(product_type)

Categories

Resources