Reputation: 622
i want to know how to remove a specific dict from this list if user status equals "offline" and order type equals "buy" (iterating over it with for loop and modifying the list while iterating produces an exception because of the list pointer)
mylist = [
{
"user": {"status": "offline"},
"order_type": "buy"
},
{
"user": {"status": "online"},
"order_type": "sell"
}
]
Upvotes: 1
Views: 46
Reputation: 36662
You can re-create the list without undesired elements:
mylist = [key_values for key_values in mylist if key_values['user']['status'] != 'offline']
(*) do not name your variables using reserved keywords.
Upvotes: 1
Reputation: 2055
seq = [
{
"user": {"status": "offline"},
"order_type": "buy"
},
{ "user": {"status": "online"},
"order_type": "sell"
}
]
for _ in seq:
print _
if _['user']['status'] == 'offline':
seq.remove(_)
print seq
In case if you're looking for in place removal.
output:
[{'user': {'status': 'online'}, 'order_type': 'sell'}]
Upvotes: 1