We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
So I want top2 to contain only the lowest two scores (yes, I should probably have named it bottom2), but I've just added a new value to the list, so there may now be 3 scores in there. So I check and if there are 3 then I remove the last one (which will be the highest of the three and not the one I want to keep) by doing this:
xs.pop()
pop() is a function that removes the last item from a list and returns that value. But I ignore it because I don't care what it was.
But before I do that, I also remove from the scores dictionary the key and the list of names associated with the score that I'm discarding. I know it can't be the second lowest score, so I don't need to keep that list of names around. Deleting it from the dict saves memory.
Now, there is a del function that removes a key and its value from a dictionary. But there's a chance that there may not be any entry in the dictionary for this score; if score was bigger than the existing values in the list, then it's been added to the end and is about to be removed from it without there ever having been an entry for it created in. So you have to check if the key exists in the dictionary before deleting it - if you use del to try and remove a key that doesn't exist, it raises an error.
Or you can use the dictionary version of the pop() function, which takes a key as input. Like the list version of pop(), it removes an element from the collection and returns its value. If you also provide a default value (as the optional second argument), it won't complain if the key doesn't exist. I don't care what that value is, so I provide a default value of None.
So that line removes an entry from the scores dictionary if it is there; otherwise it does nothing.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Nested Lists
You are viewing a single comment's thread. Return to all comments →
On to the second line you had an issue with:
So I want top2 to contain only the lowest two scores (yes, I should probably have named it bottom2), but I've just added a new value to the list, so there may now be 3 scores in there. So I check and if there are 3 then I remove the last one (which will be the highest of the three and not the one I want to keep) by doing this:
pop() is a function that removes the last item from a list and returns that value. But I ignore it because I don't care what it was.
But before I do that, I also remove from the scores dictionary the key and the list of names associated with the score that I'm discarding. I know it can't be the second lowest score, so I don't need to keep that list of names around. Deleting it from the dict saves memory.
Now, there is a del function that removes a key and its value from a dictionary. But there's a chance that there may not be any entry in the dictionary for this score; if score was bigger than the existing values in the list, then it's been added to the end and is about to be removed from it without there ever having been an entry for it created in. So you have to check if the key exists in the dictionary before deleting it - if you use del to try and remove a key that doesn't exist, it raises an error.
Or you can use the dictionary version of the pop() function, which takes a key as input. Like the list version of pop(), it removes an element from the collection and returns its value. If you also provide a default value (as the optional second argument), it won't complain if the key doesn't exist. I don't care what that value is, so I provide a default value of None.
So that line removes an entry from the scores dictionary if it is there; otherwise it does nothing.