Create an empty set() object

Asked

Viewed 178 times

0

I want to create an object of the type set empty. For this I am using the following expression:

objeto = {}

It turns out that when I do this, an object like dict. What I have to do to create an object of the type set emptiness?

1 answer

3


Just like you put it in the title, set is a predefined class in Python, so just instantiate it as well as you do with any other class:

objeto = set()

As you have noticed, the keys define an empty dictionary, this by decision of the development team, but if you want to initialize a non-empty set, you can still use the short syntax through the keys:

objeto = {1, 2, 3}

In this case, what will differentiate the dictionary set is the presence or not of the keys separated from the value by the two points, :.

How they both use the keys, the syntax {} turns out to be ambiguous. It could be an empty dictionary; how could it be an empty set. If we consider that the use of an empty set is much less recurrent than an empty dictionary, have opted for this syntax to generate a dictionary.

  • I was instantiating everything with {}, [] or (). With this I found that I can use Dict(), list(), tuple(). Thank you! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.