I have the following list of dictionary:
item = {
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:33",
"fkey_logical_column_id": "2006:41",
"pkey_logical_column_id": "2006:51",
"fkey_column_id": "3003:9",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_column_id": "3003:17",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDERS\".\"ORDER_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:14",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"ORDERS\".\"ORDER_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_column_id": "3003:29",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"PRODUCT_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"PRODUCT_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_column_id": "3003:22",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
}
]
}
I have a search id **pkey_table_id = 3001:14 ** which is present in first item of list item["relationship"][0]["relationship"]["pkey_table_id"]. I want to loop through list with key item["relationship"] and if the match is found with **pkey_table_id = 3001:14 ** i want to delete that particular item from list.So my result would be required output
item = {
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_column_id": "3003:29",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"PRODUCT_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"PRODUCT_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_column_id": "3003:22",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
}
]
}
This is what I have tried:
id_input = 3001:14
item ['relationship'] = list(filter(lambda i: i['pkey_table_id'] != id_input , item['relationship']['relationship']))
The following should work:
id_input = '3001:14'
item["relationship"] = [
i for i in item["relationship"] if i["relationship"]["pkey_table_id"] != id_input
]
You're almost right, a bit corrected your code:
def process(item):
id_input = '3001:14'
item['relationship'] = list(filter(lambda i:
i['relationship']['pkey_table_id'] != id_input , item['relationship']))
item = {
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:33",
"fkey_logical_column_id": "2006:41",
"pkey_logical_column_id": "2006:51",
"fkey_column_id": "3003:9",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_column_id": "3003:17",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDERS\".\"ORDER_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:14",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"ORDERS\".\"ORDER_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_column_id": "3003:29",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"PRODUCT_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"PRODUCT_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_column_id": "3003:22",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
}
]
}
process(item)
print(item)
Output:
{
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": '"localhost:1521/orcl".."OT"."ORDER_ITEMS"."PRODUCT_ID"',
"pkey_column_id": "3003:29",
"pkey_column_name": '"localhost:1521/orcl".."OT"."PRODUCTS"."PRODUCT_ID"',
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": '"ofline_online_test"."ORDER_ITEMS"."PRODUCT_ID"',
"pkey_business_column_name": '"ofline_online_test"."PRODUCTS"."PRODUCT_ID"',
"from_type": "0..1",
"to_type": "n..n",
},
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": '"localhost:1521/orcl".."OT"."PRODUCTS"."CATEGORY_ID"',
"pkey_column_id": "3003:22",
"pkey_column_name": '"localhost:1521/orcl".."OT"."PRODUCT_CATEGORIES"."CATEGORY_ID"',
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": '"ofline_online_test"."PRODUCTS"."CATEGORY_ID"',
"pkey_business_column_name": '"ofline_online_test"."PRODUCT_CATEGORIES"."CATEGORY_ID"',
"from_type": "0..1",
"to_type": "n..n",
},
},
]
}
Here's the answer with a little modification to your own code:
id_input = "3001:14"
item['relationship'] = list(filter(lambda i: i['relationship']['pkey_table_id'] != id_input, item['relationship']))
id_input
is a valid string!This should work
val_to_remove = '3001:25'
for i in item['relationship']:
if i['relationship']['pkey_table_id'] == val_to_remove :
item['relationship'].remove(i)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.