Python Beginner: How to insert a list of dictionaries inside a dictionary that contains an empty list? - list

I have this dictionary named element, and a list of dictionaries named l1
l1 = [{"parent_id": "",
"productid": "6x2562",
"sequence_no": "1",
"quantity": "8.000",
"listprice": "",
"discount_percent": "",
"discount_amount": "",
"comment": "new list item 1",
"description": "some text",
"lineitem_id": "1",
"tax4": "",
"id": ""},
{"parent_id": "",
"productid": "6x2562",
"sequence_no": "1",
"quantity": "8.000",
"listprice": "",
"discount_percent": "",
"discount_amount": "",
"comment": "new list item 1",
"description": "some text",
"lineitem_id": "1",
"tax4": "",
"id": ""}]
element = {
"subject": "testtest ",
"potential_id": "",
"quotestage": "Created",
"validtill": "2023-03-11 10:48:10.339414",
"contact_id": "4x540",
"id": "",
"LineItems": []
}
element dictionary contains an empty list as a value to a key named LineItems. I am inserting a list of dictionaries (l1) in place of this empty list using following code:
element['LineItems'].append(l1)
print(element)
The problem I am having is that I am getting the following result which contains two [ ] brackets rather then one.
My current output:
{'subject': 'testtest ', 'potential_id': '', 'quotestage': 'Created', 'validtill': '2023-03-11 10:48:10.339414', 'contact_id': '4x540', 'id': '', 'LineItems': [[{'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}, {'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}]]}
Expected Output:
{'subject': 'testtest ', 'potential_id': '', 'quotestage': 'Created', 'validtill': '2023-03-11 10:48:10.339414', 'contact_id': '4x540', 'id': '', 'LineItems': [{'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}, {'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}]}
I have no idea how to do this as I have been searching for whole day now.
Any help will be appreciated.
Thanks in advance!

Related

How to group_by Queryset distinct by id and append values Django

In Django i have the results i want but it returns separated data in same ids
How to groupby ids in a list of dicts?
Use pandas to fix this, but it doesn't work quite right. What I need is a simple dictionary list where if there is a repeated id, the information that is different is added as the value of that key and if there are several values, it is stored in a list. So as I show below in the result I want
i have this:
<
QuerySet[{
'id': 7086098,
'action_plan': None,
'comment': None
}, {
'id': 7105838,
'action_plan': 'foo',
'comment': None
}, {
'id': 7105838,
'action_plan': 'foos2',
'comment': None
}, {
'id': 7169339,
'action_plan': 'xxxxxx',
'comment': None
}, {
'id': 7169346,
'action_plan': 'report',
'comment': None
}, {
'id': 7169346,
'action_plan': 'zxczxczxczc',
'comment': None
}, {
'id': 7622793,
'action_plan': 'foofoo',
'comment': None
}, {
'id': 7622793,
'action_plan': 'role play',
'comment': None
}, {
'id': 7723661,
'action_plan': 'google',
'comment': 'chrome'
}, {
'id': 7723661,
'action_plan': 'netscape',
'comment': None
}, {
'id': 7723661,
'action_plan': 'urra',
'comment': 'firefox'
}, {
'id': 7723661,
'action_plan': 'sdasd',
'comment': None
}] >
i want to get this:
[{
'id': 7086098,
'action_plan': None,
'comment': None
}, {
'id': 7105838,
'action_plan': ['foo', 'foos2'],
'comment': [None, None]
}, {
'id': 7169339,
'action_plan': 'xxxxxx',
'comment': None
}, {
'id': 7169346,
'action_plan': ['report', 'zxczxczxczc'],
'comment': [None, None]
}, {
'id': 7622793,
'action_plan': ['foofoo', 'role play'],
'comment': [None, None]
}, {
'id': 7723661,
'action_plan': ['google', 'netscape', 'urra', 'sdasd'],
'comment': ['chrome', None, 'firefox', None]
}]

Check if a set of key/values in dict list is in another list

I have a list as follows:
[
{
'Name': 'Name1',
'Address': 'Address1'
},
{
'Name': 'Name2',
'Postcode': 'MyPostcode'
}
]
And another list like this:
[
{
'Name': 'Name1',
'data' : {
'Address': 'Address1',
'Postcode': 'Whatever'
},
{
'Name': 'Name2',
'data' : {
'Address': 'Whatever',
'Postcode': 'MyPostcode'
},
{
'Name': 'Name3',
'data' : {
'Address': 'Whatever',
'Postcode': 'Whatever'
},
]
For any of the items in the first list, I need to check if that combination of key/value exists in the second, and if so, delete it from the second
I can do it in multiple lines of codes, with different for loops, but probably there is a wiser way of doing it. Can anybody suggest an elegant solution?
In the example above, it should delete the two first dicts from the list and return a list only with the third one
[
{
'Name': 'Name3',
'data' : {
'Address': 'Whatever',
'Postcode': 'Whatever'
}
]
Thanks
Try:
lst1 = [
{"Name": "Name1", "Address": "Address1"},
{"Name": "Name2", "Postcode": "MyPostcode"},
]
lst2 = [
{"Name": "Name1", "data": {"Address": "Address1", "Postcode": "Whatever"}},
{
"Name": "Name2",
"data": {"Address": "Whatever", "Postcode": "MyPostcode"},
},
{"Name": "Name3", "data": {"Address": "Whatever", "Postcode": "Whatever"}},
]
out = []
for d2 in lst2:
t = set({"Name": d2["Name"], **d2["data"]}.items())
for d1 in lst1:
if t.issuperset(d1.items()):
break
else:
out.append(d2)
print(out)
Prints:
[{'Name': 'Name3', 'data': {'Address': 'Whatever', 'Postcode': 'Whatever'}}]

Dart/Flutter - How to delete key-value-pairs of element of Map inside a List?

How can I can delete a specific key-value-pair in that type of List<Map<String, dynamic>> for each map.
For example:
List<Map<String, dynamic>> before the operation:
List<Map<String, dynamic>> currencies = [
{
'id': 1,
'name': 'coin',
'desc': 'This is just a blindtext.',
'order': 101,
'icon': 'https://icon1.jpg'
},
{
'id': 2,
'name': 'karma',
'desc': 'This is just a blindtext.',
'order': 102,
'icon': 'https://icon2.jpg'
},
{
'id': 3,
'name': 'laurel',
'desc': 'This is just a blindtext.',
'order': 104,
'icon': 'https://icon3.jpg'
},
];
List<Map<String, dynamic>> after the operation I am searching for:
List<Map<String, dynamic>> currencies = [
{
'id': 1,
'name': 'coin',
'icon': 'https://icon1.jpg'
},
{
'id': 2,
'name': 'karma',
'icon': 'https://icon2.jpg'
},
{
'id': 3,
'name': 'laurel',
'icon': 'https://icon3.jpg'
},
];
So basically, I deleted "unwanted" key-value-pairs.
Any suggestions?
Thanks in advance!!
currencies.forEach((item) => item..remove("order")..remove("desc"));

How do i get a specific range for "List<List<Map<String, dynamic>>>"?

I want to write a Sudoku App for which i have a List in a List of Maps to highlight and show the integers in the playfield.
My list looks like this:
List<List<Map<String, dynamic>>> output = [
[
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
],
[
{'integer': '', 'color': Colors.purple},
{'integer': '', 'color': Colors.purple},
...
So i want to change the color of a specific Area. I have tried some googlin and came up with this and some variations:
selectField(indexBlock, index) {
output[indexBlock][index]['color'] = Colors.white;
output[indexBlock].getRange(0, 8).['color'] = Colors.purpleAccent; // this part with getRange is not working like i want it to
notifyListeners();
}

Facebook Messenger Bot - Invalid URL button fields for List Template

I have been working to send a list to user containing some data. I am following facebook's doc to setup my request payload. However, I am getting the following error:
{'error': {
'message': '(#100) Invalid URL button fields provided. Please check documentation for details.',
'type': 'OAuthException',
'code': 100,
'error_subcode': 2018125, 'fbtrace_id': 'GZFFcM+j5e/'}}
Here is my JSON Payload:
{'recipient': {'id': 'MY_MESSENGER_ID'},
'message':
{'attachment':
{'type': 'template',
'payload':
{'template_type': 'list',
'top_element_style': 'compact',
'elements':
[{'title': 'Hello 1', 'subtitle': 'Subtitle 1',
'buttons':
[{'title': 'View', 'type': 'web_url',
'url': 'https://www.medium.com/',
'messenger_extensions': 'false',
'webview_height_ratio': 'full',
'fallback_url': 'https://www.medium.com/'}],
'default_action':
{'title': 'View', 'type': 'web_url',
'url': 'https://www.medium.com/',
'messenger_extensions': 'false',
'webview_height_ratio': 'full',
'fallback_url': 'https://www.medium.com/'}},
{'title': 'Hello 2', 'subtitle': 'Subtitle 2',
'image_url': 'https://cdn-images-1.medium.com/1*Vkf6A8Mb0wBoL3Fw1u0paA.jpeg',
'buttons':
[{'title': 'View', 'type': 'web_url',
'url': 'https://www.medium.com/',
'messenger_extensions': 'false',
'webview_height_ratio': 'full',
'fallback_url': 'https://www.medium.com/'}],
'default_action':
{'title': 'View', 'type': 'web_url',
'url': 'https://www.medium.com/',
'messenger_extensions': 'false',
'webview_height_ratio': 'full',
'fallback_url': 'https://www.medium.com/'}}]}}}}
I have checked, re-checked it multiple times. PLUS, I have sent the facebook's example json from the doc but I have got the same reply. Please take a look and let me know where I am stuck!
This is my end url:
"https://graph.facebook.com/v2.6/me/messages?access_token="
Thanks in advance!
Your request has two issues:
For default_action, you can set fallback_url only if messenger_extensions:true
default_action cannot have a title prop.
Try this:
{
"recipient": {
"id": "{{PSID}}"
},
"message": {
"attachment": {
"type": "template",
"payload": {
"template_type": "list",
"top_element_style": "compact",
"elements": [{
"title": "Hello 1",
"subtitle": "Subtitle 1",
"buttons": [{
"title": "View",
"type": "web_url",
"url": "https://www.medium.com/",
"messenger_extensions": "false",
"webview_height_ratio": "full"
}],
"default_action": {
"type": "web_url",
"url": "https://www.medium.com/",
"messenger_extensions": "false",
"webview_height_ratio": "full"
}
},
{
"title": "Hello 2",
"subtitle": "Subtitle 2",
"image_url": "https://cdn-images-1.medium.com/1*Vkf6A8Mb0wBoL3Fw1u0paA.jpeg",
"buttons": [{
"title": "View",
"type": "web_url",
"url": "https://www.medium.com/",
"messenger_extensions": "false",
"webview_height_ratio": "full"
}],
"default_action": {
"type": "web_url",
"url": "https://www.medium.com/",
"messenger_extensions": "false",
"webview_height_ratio": "full"
}
}
]
}
}
}
}