0
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

Lost
  • 17
  • 3
  • 1
    What if multiple elements have that same title? What if no elements have the matching title? – wim Nov 04 '20 at 03:40

3 Answers3

2

What you have is a list of dicts, 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"]
ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
  • Can you help me under the code above. What is d for d and if d['title'] what does it do? – Lost Nov 04 '20 at 03:50
  • @Lost: You can start by reading either this question [What does “list comprehension” mean? How does it work and how can I use it?](https://stackoverflow.com/q/34835951/364696) and/or the [Python tutorial on list comprehensions](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions). Short version is that it's a way of building a new `list` by iterating an existing iterable; the `d` on the far left is the item produced, the `if` on the right filters which items should be produced; the middle reads just like a `for` loop. – ShadowRanger Nov 04 '20 at 03:55
1

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!

0

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}]
'''
Dharman
  • 21,838
  • 18
  • 57
  • 107