converting list to dictionary in a python - python

I have a data in a list like below:
[
{'id': 1, 'first_name': 'Jeanette', 'last_name': 'Penddreth', 'email': 'jpenddreth0#census.gov', 'gender': 'Female', 'ip_address': '26.58.193.2'},
{'id': 2, 'first_name': 'Giavani', 'last_name': 'Frediani', 'email': 'gfrediani1#senate.gov', 'gender': 'Male', 'ip_address': '229.179.4.212'},
{'id': 3, 'first_name': 'Noell', 'last_name': 'Bea', 'email': 'nbea2#imageshack.us', 'gender': 'Female', 'ip_address': '180.66.162.255'},
{'id': 4, 'first_name': 'Willard', 'last_name': 'Valek', 'email': 'wvalek3#vk.com', 'gender': 'Male', 'ip_address': '67.76.188.26'}
]
I am loading the data into the dynamoDb. It is failing with the error type: <class 'list'>, valid types: <class 'dict'>: ParamValidationError.
How do I convert the above list into a dictionary?
EDIT Code used:
import boto3
import json
s3_client=boto3.client('s3')
dynamodb=boto3.resource('dynamodb')
def lambda_handler(event, context):
bucket=event['Records'][0]['s3']['bucket']['name']
json_filename=event['Records'][0]['s3']['object']['key']
json_object=s3_client.get_object(Bucket=bucket,Key=json_filename)
jsonFileReader=json_object['Body'].read()
jsonDictionary=json.loads(jsonFileReader)
table=dynamodb.Table('EMPLOYEE_DETAILS')
table.put_item(Item=jsonDictionary)
return 'Done'

I'm not familiar with dynamodb, but I imagine this will work. If your JSON is a list, then you need to iterate through the items in your list, adding each one to the table.
Replace the line:
table.put_item(Item=jsonDictionary)
with:
if type(jsonDictionary) == type([]):
# It is a list - iterate through it
for item in jsonDictionary:
table.put_item(Item=item)
else:
table.put_item(Item=jsonDictionary)

Related

Merge dictionaries in a list of dictionaries by combining the values

Help me please.
I have query set from Model.objects.value('name', 'language__name') and it give me the list of dictionary:
list = [{'id': 1, 'name': 'Adel', 'language': 'С#'},
{'id': 1, 'name': 'Adel', 'language': 'Python'},
{'id': 5, 'name': 'Dora', 'language': 'С#'},
{'id': 5, 'name': 'Dora', 'language': 'Java'},
{'id': 6, 'name': 'Dars', 'language': 'Python'}];
how can I to do a list of dictionary but to unit key and value?
I want to get like this:
list = [{'id': 1, 'name': 'Adel', 'language':['С#','Python']},
{'id': 5, 'name': 'Dora', 'language': ['С#','Java']},
{'id': 6, 'name': 'Dars', 'language': 'Python'}];
I tried this:
mapping = {}
for d in qs:
try:
entry = mapping[d['id']] # raises KeyError
entry['language__name'].append(d['language__name']) # raises AttributeError
except KeyError:
mapping[d['id']] = d
except AttributeError:
entry['language__name'] = [entry['language__name'], d['language__name']]
print(list(mapping.values()))
You can iterate over lst and create a dictionary out where the keys correspond to "id" values in the dicts in lst and the values are dicts. In each iteration, check if the value under "language" key is a list or not and append to the list if it's a list, create a list, if not. Finally, pass the values of out to a list constructor for the final outcome.
out = {}
for d in lst:
if d['id'] in out:
if isinstance(out[d['id']]['language'], list):
out[d['id']]['language'].append(d['language'])
else:
out[d['id']]['language'] = [out[d['id']]['language'], d['language']]
else:
out[d['id']] = d
out = list(out.values())
Output:
[{'id': 1, 'name': 'Adel', 'language': ['С#', 'Python']},
{'id': 5, 'name': 'Dora', 'language': ['С#', 'Java']},
{'id': 6, 'name': 'Dars', 'language': 'Python'}]

How do I access a specific value in a nested Python dictionary?

I am trying to figure out how to filter for the dictionaries that have a status of "awaiting_delivery". I am not sure how to do this (or if it is impossible). I am new to python and programming. I am using Python 3.8.5 on VS Code on Ubuntu 20.04. The data below is sample data that I created that resembles json data from an API. Any help on how to filter for "status" would be great. Thank you.
nested_dict = {
'list_data': [
{
'id': 189530,
'total': 40.05,
'user_data': {
'id': 1001,
'first_name': 'jane',
'last_name': 'doe'
},
'status': 'future_delivery'
},
{
'id': 286524,
'total': 264.89,
'user_data': {
'id': 1002,
'first_name': 'john',
'last_name': 'doe'
},
'status': 'awaiting_delivery'
},
{
'id': 368725,
'total': 1054.98,
'user_data': {
'id': 1003,
'first_name': 'chris',
'last_name': 'nobody'
},
'status': 'awaiting_delivery'
},
{
'id': 422955,
'total': 4892.78,
'user_data': {
'id': 1004,
'first_name': 'mary',
'last_name': 'madeup'
},
'status': 'future_delivery'
}
],
'current_page': 1,
'total': 2,
'first': 1,
'last': 5,
'per_page': 20
}
#confirm that nested_dict is a dictionary
print(type(nested_dict))
#create a list(int_list) from the nested_dict dictionary
int_list = nested_dict['list_data']
#confirm that int_list is a list
print(type(int_list))
#create the int_dict dictionary from the int_list list
for int_dict in int_list:
print(int_dict)
#this is my attempt at filtering the int_dict dictionar for all orders with a status of awaiting_delivery
for order in int_dict:
int_dict.get('status')
print(order)
Output from Terminal Follows:
<class 'dict'>
<class 'list'>
{'id': 189530, 'total': 40.05, 'user_data': {'id': 1001, 'first_name': 'jane', 'last_name': 'doe'}, 'status': 'future_delivery'}
{'id': 286524, 'total': 264.89, 'user_data': {'id': 1002, 'first_name': 'john', 'last_name': 'doe'}, 'status': 'awaiting_delivery'}
{'id': 368725, 'total': 1054.98, 'user_data': {'id': 1003, 'first_name': 'chris', 'last_name': 'nobody'}, 'status': 'awaiting_delivery'}
{'id': 422955, 'total': 4892.78, 'user_data': {'id': 1004, 'first_name': 'mary', 'last_name': 'madeup'}, 'status': 'future_delivery'}
id
total
user_data
status
You can obtain a filtered list of dicts by doing conditional list comprehension on your list of dicts:
# filter the data
list_data_filtered = [entry for entry in nested_dict['list_data']
if entry['status'] == 'awaiting_delivery']
# print out the results
for entry in list_data_filtered:
print(entry)
# results
# {'id': 286524, 'total': 264.89, 'user_data': {'id': 1002, 'first_name': 'john', 'last_name': 'doe'}, 'status': 'awaiting_delivery'}
# {'id': 368725, 'total': 1054.98, 'user_data': {'id': 1003, 'first_name': 'chris', 'last_name': 'nobody'}, 'status': 'awaiting_delivery'}

Make a list of first item in every element of a dictionary

I am working with a dictionary from a file with an import. This is the dictionary:
[{'id': 76001,
'full_name': 'Alaa Abdelnaby',
'first_name': 'Alaa',
'last_name': 'Abdelnaby',
'is_active': False},
{'id': 76002,
'full_name': 'Zaid Abdul-Aziz',
'first_name': 'Zaid',
'last_name': 'Abdul-Aziz',
'is_active': False},
{'id': 76003,
'full_name': 'Kareem Abdul-Jabbar',
'first_name': 'Kareem',
'last_name': 'Abdul-Jabbar',
'is_active': False}]
What I want to do is get a list out of all the IDs:
player_ids = [76001,76002, 76003]
I have tried:
player_ids = []
for i in player_dict:
player_ids.append(player_dict[i]['id'])
but I get the error
TypeError: list indices must be integers or slices, not dict
So I get that 'i' is not the place but the actual item I am calling in the dictionary? But I'm not able to make much sense of this based on what I have read.
The pythonic way to do this is with a list comprehension. For example:
player_ids = [dict['id'] for dict in player_dict]
This basically loops over all dictionaries in the player_dict, which is actually a list in your case, and for every dictionary gets the item with key 'id'.
You can try list comprehension:
>>> [d['id'] for d in my_list]
[76001, 76002, 76003]
Here is how you can use a list comprehension:
player_dict = [{'id': 76001,
'full_name': 'Alaa Abdelnaby',
'first_name': 'Alaa',
'last_name': 'Abdelnaby',
'is_active': False},
{'id': 76002,
'full_name': 'Zaid Abdul-Aziz',
'first_name': 'Zaid',
'last_name': 'Abdul-Aziz',
'is_active': False},
{'id': 76003,
'full_name': 'Kareem Abdul-Jabbar',
'first_name': 'Kareem',
'last_name': 'Abdul-Jabbar',
'is_active': False}]
player_ids = [d['id'] for d in player_dict]
print(player_ids)
Output:
[76001, 76002, 76003]
Just append
player_ids.append(i['id'])

Adding a dictionary to the end of a list of tuples in a dictionary

I'm working on a problem and I'm a little confused about where to begin.
Here is the list I'm working with:
customers = list();
customer_list.append( {{'id':12345, 'first_name':'John', 'last_name':'Anderson','orders':[('order_1',300),('order_2',255),('order_3',79)]})
customer_list.append( {{'id':12346, 'first_name':'Mary',
'last_name':'Smith','orders':[('Order_1' 400),('order_2',199),('order_3',49)]})
I need to pass a dictionary with an order name and number through my list. If the order number in the dictionary doesn't exist in the list it should be added to the end of the list. If the order number does exist it should not be added. Additionally, if the assignment was added it should return true and if not it should return false.
I'm really struggling to get started with this, any help would be appreciated. This is as far as I've gotten so far.
order_dict = {order_1: 29, order_4, 99}
#I want to add order_4 to the end of my list of tuples
def add_order(customers)
for c in customers:
You can iterate through the customers list of dicts to look for a customer name matching that of the order. If the matching customer name is found, look for the matching order number, and if found, break out of for-else constructs to return False; if a matching order number is not found, append the order number to the orders key of the customer of the matching name. If no customer is found with a matching name, append to the customers list with the new order details and with an incremental id.
def add_order(customers, order):
for customer in customers:
if order['first_name'] == customer['first_name'] and order['last_name'] == customer['last_name']:
for _, order_number in customer['orders']:
if order['number'] == order_number:
break
else:
customer['orders'].append(('order_%d' % (len(customer['orders']) + 1), order['number']))
return True
break
else:
customers.append({
'id': customers[-1]['id'] + 1 if customers else 1,
'first_name': order['first_name'],
'last_name': order['last_name'],
'orders': [('order_1', order['number'])]
})
return True
return False
so that:
customers = [{'id': 12345, 'first_name': 'John', 'last_name': 'Anderson',
'orders': [('order_1', 300), ('order_2', 255), ('order_3', 79)]},
{'id': 12346, 'first_name': 'Mary', 'last_name': 'Smith',
'orders': [('order_1', 400), ('order_2', 199), ('order_3', 49)]}]
print(add_order(customers, {'first_name': 'Mary', 'last_name': 'Smith', 'number': 199}))
print(add_order(customers, {'first_name': 'Mary', 'last_name': 'Smith', 'number': 200}))
print(add_order(customers, {'first_name': 'Mary', 'last_name': 'Brown', 'number': 201}))
from pprint import pprint
pprint(customers)
would output:
False
True
True
[{'first_name': 'John',
'id': 12345,
'last_name': 'Anderson',
'orders': [('order_1', 300), ('order_2', 255), ('order_3', 79)]},
{'first_name': 'Mary',
'id': 12346,
'last_name': 'Smith',
'orders': [('order_1', 400),
('order_2', 199),
('order_3', 49),
('order_4', 200)]},
{'first_name': 'Mary',
'id': 12347,
'last_name': 'Brown',
'orders': [('order_1', 201)]}]

Getting a python dict.keys trail for each item

I have a python dict that looks like this
{'data': [{'data': [{'data': 'gen1', 'name': 'objectID'},
{'data': 'familyX', 'name': 'family'}],
'name': 'An-instance-of-A'},
{'data': [{'data': 'gen2', 'name': 'objectID'},
{'data': 'familyY', 'name': 'family'},
{'data': [{'data': [{'data': '21',
'name': 'objectID'},
{'data': 'name-for-21',
'name': 'name'},
{'data': 'no-name', 'name': None}],
'name': 'An-instance-of-X:'},
{'data': [{'data': '22',
'name': 'objectID'}],
'name': 'An-instance-of-X:'}],
'name': 'List-of-2-X-elements:'}],
'name': 'An-instance-of-A'}],
'name': 'main'}
The structure is repeating and its rule is like:
A dict contains 'name' and 'data'
'data' can contain a list of dicts
If 'data' is not a list, it is a value I need.
'name' is a just a name
The problem is that for each value, I need to know every info for each parent.
So at the end, I need to print a list with items that looks something like:
objectID=gen2 family=familyY An-instance-of-X_objectID=21 An-instance-of-X_name=name-for-21
Edit: This is only one of several lines I want as the output. I need one line like this for each item that doesn’t have a dict as 'data'.
So, for each data that is not a dict, traverse up, find info and print it..
I don't know every function in modules like itertools and collections. But is there something in there I can use? What is this called (when I am trying to do research on my own)?
I can find many "flatten dict" methods, but not like this, not when I have 'data', 'name' like this..
This is a wonderful example what recursion is good for:
input_ = {'data': [{'data': [{'data': 'gen1', 'name': 'objectID'},
{'data': 'familyX', 'name': 'family'}],
'name': 'An-instance-of-A'},
{'data': [{'data': 'gen2', 'name': 'objectID'},
{'data': 'familyY', 'name': 'family'},
{'data': [{'data': [{'data': '21',
'name': 'objectID'},
{'data': 'name-for-21',
'name': 'name'},
{'data': 'no-name', 'name': None}],
'name': 'An-instance-of-X:'},
{'data': [{'data': '22',
'name': 'objectID'}],
'name': 'An-instance-of-X:'}],
'name': 'List-of-2-X-elements:'}],
'name': 'An-instance-of-A'}],
'name': 'main'}
def parse_dict(d, predecessors, output):
"""Recurse into dict and fill list of path-value-pairs"""
data = d["data"]
name = d["name"]
name = name.strip(":") if type(name) is str else name
if type(data) is list:
for d_ in data:
parse_dict(d_, predecessors + [name], output)
else:
output.append(("_".join(map(str,predecessors+[name])), data))
result = []
parse_dict(input_, [], result)
print "\n".join(map(lambda x: "%s=%s"%(x[0],x[1]),result))
Output:
main_An-instance-of-A_objectID=gen1
main_An-instance-of-A_family=familyX
main_An-instance-of-A_objectID=gen2
main_An-instance-of-A_family=familyY
main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_objectID=21
main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_name=name-for-21
main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_None=no-name
main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_objectID=22
I hope I understood your requirements correctly. If you don't want to join the paths into strings, you can keep the list of predecessors instead.
Greetings,
Thorsten

Categories

Resources