Reputation: 19
video_ads_list = [
{"title": "Get the newest iPhone 6", "company": "Apple", "views": 389824},
{"title": "Samsung Galaxy S2 out now!", "company": "Saumsung", "views": 230123}
]
# Show how you would clean out this list with the use of del function
May I know how to delete say the title "Get the newest iPhone 6", is it possible to code such that if we delete "Get the newest iPhone 6", we will deplete all the parts like "Apple" and the number of views?
Thanks
Upvotes: 0
Views: 91
Reputation: 13
Over in the example you have a list containing dictionaries. The dictionary keys' values can both be deleted with: del my_dict["key"]
. To delete the dictionaries as a whole however, you can loop through the list to delete them if they have certain attributes.
#Original ad list
video_ads_list = [
{"title": "Get the newest iPhone 6", "company": "Apple", "views": 389824},
{"title": "Samsung Galaxy S2 out now!", "company": "Saumsung", "views": 230123}
]
#List of ads you want to remove by title
unaccepted = ["Get the newest iPhone 6"]
video_ads_list = [i for i in video_ads_list if i["title"] not in unaccepted]
#To understand the above, lets put it in a normal loop format:
#i will be a variable we declare that goes through the list called video_ads_list.
for i in video_ads_list:
'''
i is now an item in video_ads_list, which would be a dictionary in this case.
i["title"] means getting the value of the key, "title".
So if the value of the key, title, of item i in video_ads_list is not in the unnacepted list, then we keep it.
'''
if i["title"] not in unaccepted:
pass
else:
video_ads_list.remove(i)
#We now remove the item in this list that corresponds to i.
'''
Going back to the example, video_ads_list = [i for i in video_ads_list if i["title"] not in unaccepted], i, each element
looping through the list stays if its item's title is not in the unaccepted list.
'''
#Output the final result
print(video_ads_list)
'''
The output looks like this:
[{'title': 'Samsung Galaxy S2 out now!', 'company': 'Saumsung', 'views': 230123}]
'''
Upvotes: 0
Reputation: 145
The Objective you are trying to do is not delete from a dictionary, but delete a dictionary from a list, you might already know this but the {}
is a dictionary and a []
is a list, therefore you have a []
list of dictionaries {}
giving you a [{}, {}]
and you want to delete one {}
dict from the []
, so you want a [{}]
instead of a [{}, {}]
in your case you want to go from:
[{'title': 'Get the newest iPhone 6', 'company': 'Apple', 'views': 389824}, {'title': 'Samsung Galaxy S2 out now!', 'company': 'Saumsung', 'views': 230123}]
to this (removing the apple ad):
[{'title': 'Samsung Galaxy S2 out now!', 'company': 'Saumsung', 'views': 230123}]
to do that you need to remove a dictionary (sorry if this is annoying since im bringing it up repeatedly but I am stressing on this point) and you would do that by making a new dict or re-initializing the dict to not contain the element, or pop the element out with the index. If you have the index of the ad then you can pop it out, if you dont then search for it and exclude it..
How you would pop it out:
video_ads_list.pop(0)
How you would exclude it:
video_ads_list = [x for x in video_ads_list if not x['title'] == "Get the newest iPhone 6"]
if I didnt get your question and it was to update a dict in the list then I would go for:
video_ads_list = [x if not x['title'] == "Get the newest iPhone 6" else updated_dict for x in video_ads_list]
Thank You!
Upvotes: 1
Reputation: 155363
What you have is a list
of dict
s, not a dict
. Removing the offending item requires you to loop and test, so it's fairly inefficient if the list
is long, but not complicated. You wouldn't do it with del
though; it's tricky to do properly since you'd be iterating the list
while mutating it (a no-no). Simplest solution is a list comprehension:
video_ads_list = [d for d in video_ads_list if d['title'] != "Get the newest iPhone 6"]
Upvotes: 2