Making Custom Containers

Could anyone help me with this code:

class TagCloud:
    def __init__(self):
        self.tags = {}

    def add(self, tag):
        self.tags[tag.lower()] = self.tags.get(tag.lower(), 0) + 1

What is really confusing is tag as in these:

  1. def add(self, tag): I still have blurry understanding here
  2. self.tags[tag]: what does tag in [] mean? I mean could anyone explain that in simple language WITH EXAMPLES. I remember we had this earlier as well, but could not get it. Why do we have to put it into []?

It is the third day I am stuck in here. Cannot continue ahead without understanding this part.

Any help would be for sure appreciated.

1 Like

The def add(self, tag) is saying that the add method takes a tag parameter. So you call the method like this:


In this example, 'Hello' is passed in as the tag parameter. Then inside the add method, wherever you see tag, it’s actually working with 'Hello'.

So this…




Which becomes…


The reason for the [] is that self.tags is a dictionary, and to find an item in a dictionary, you look up the item by its key. The thing in the [] is the key. So let’s say we have a dictionary that looks like this:

myDictionary = {
  'hello': 2,
  'world': 1

This is a dictionary with two entries. The first entry is referred to by the key 'hello' and the second entry is referred to by the key 'world'. So for example with this:


you will see that it prints out 2, which is the value of the entry referred to by the 'hello' key. Just like with a real-life dictionary, you look up items by their key.

To see this in action, you might add a print to your code, like this:

class TagCloud:
    def __init__(self):
        self.tags = {}

    def add(self, tag):
        self.tags[tag.lower()] = self.tags.get(tag.lower(), 0) + 1

myTagCloud = TagCloud()

The result of this code is:

{'hello': 1}
{'hello': 1, 'world': 1}
{'hello': 2, 'world': 1}

I do appreciate your help! You did great job explaining this all in accurate details! I have not fully understood this, but I got what minuses I have. Right now I am covering it all over again.
containers: lists/dictionaries.

Appreciate your help.

A question though, have you learnt what you explained here on this platform ?(

No, I haven’t taken Mosh’s Python courses (I have taken his JavaScript courses and I’m slowly working my way through a few others). Everything I know about Python I’ve learned while answering questions on this forum! The concepts are similar to what I’m familiar with in JavaScript and other languages, so thankfully I’m able to apply that knowledge to Python. I use this site to run Python code and to verify that my answers are correct.

I have just understood only self.tags[tag.lower()] = self.tags.get(key, 0)

Yet, don’t know how to use + 1 .

Do you think you can help me?

... = self.tag.get(tag.lower(), 0) + 1 

The TagCloud container’s job is to keep track of the number of times each tag has been added. The + 1 is just adding 1 to the current count. For example, the first time we add 'Hello', this line of code

self.tags[tag.lower()] = self.tags.get(tag.lower(), 0) + 1

ends up looking like this

self.tags['hello'] = 0 + 1

The second time we add Hello, the line of code ends up looking like this

self.tags['hello'] = 1 + 1

And the third time

self.tags['hello'] = 2 + 1

And so on. The + 1 is always just adding 1 to the count.

1 Like