Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dict and set (python)

So, I know that this,

a = {}  # dict 

constructs an empty dictionary. Now, I also picked up that this,

b = {1, 2, 3}  # set 

creates a set. This can easily be verified, as,

>>>print(type(a)) <class 'dict'>  >>>print(type(b)) <class 'set'> 

While I understand what it does, I fail to see why we use 'set notation' for empty dictionaries. I tried to find some more information about the logic behind this in the set and dict sections of the manual, but sadly, I got nothing out of it.

Could anyone explain to me why we do this in this way? Is it for historical reasons, or am I missing something blatantly obvious?

like image 900
Niels Wouda Avatar asked Dec 19 '15 12:12

Niels Wouda


People also ask

Which is better set or dictionary in Python?

Set will not allow duplicate elements and dictionary doesn't allow duplicate keys. Tuple can be created using tuple() function. Dictionary can be created using dict() function. List is mutable i.e we can make any changes in list.

Which is faster dict or set in Python?

The fastest way to repeatedly lookup data with millions of entries in Python is using dictionaries. Because dictionaries are the built-in mapping type in Python thereby they are highly optimized.

Is dict faster than set?

Basis of Dictionaries and Sets Compared with lists and tuples, the performance of dictionaries is better, especially for search, add, and delete operations. A dictionary can be completed within a constant time complexity.

What is the difference between set and dictionary in Python?

# Set: A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. ... Dictionary: in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair.

What is the difference between Dict and {} in Python?

One other difference between {} and dict is that dict always allocates a new dictionary (even if the contents are static) whereas {} doesn't always do so (see mgood's answer for when and why):

What is the difference between a set and a dict?

Well, a set is like a dict with keys but no values, and they're both implemented using a hash table. But yes, it's a little annoying that the {} notation denotes an empty dict rather than an empty set, but that's a historical artifact.

What is a set in Python?

A Python set is a slightly different concept from a list or a tuple. A set, in Python, is just like the mathematical set. It does not hold duplicate values and is unordered.


2 Answers

There were no set literals in Python 2, historically curly braces were only used for dictionaries. Sets could be produced from lists (or any iterables):

set([1, 2, 3]) set([i for i in range(1, 3)]) 

Python 3 introduced set literals and comprehensions (see PEP-3100) which allowed us to avoid intermediate lists:

{1, 2, 3} {i for i in range(1, 3)} 

The empty set form, however, was reserved for dictionaries due to backwards compatibility. References from [Python-3000] sets in P3K? states:

I'm sure we can work something out --- I agree, {} for empty set and {:} for empty dict would be ideal, were it not for backward compatibility. I liked the "special empty object" idea when I first wrote the PEP (i.e., have {} be something that could turn into either a set or dict), but one of the instructors here convinced me that it would just lead to confusion in newcomers' minds (as well as being a pain to implement).

The following message describes these rules better:

I think Guido had the best solution. Use set() for empty sets, use {} for empty dicts, use {genexp} for set comprehensions/displays, use {1,2,3} for explicit set literals, and use {k1:v1, k2:v2} for dict literals. We can always add {/} later if demand exceeds distaste.

like image 144
myaut Avatar answered Oct 13 '22 09:10

myaut


The syntax is not the same. Dictionaries used curly braces the first and you specify key-value pairs, where the key and value are separated by a colon:

>>> {'foo': 'bar'} {'foo': 'bar'} >>> type(_) <type 'dict'> 

Sets were added to the language later on, and the {..} curly brace notation only names elements, not pairs:

>>> {'foo'} set(['foo']) >>> type(_) <type 'set'> 

Note that in Python 2, the interpreter echoes the object using the set() callable. That's also how you specify an empty set:

>>> emptyset = set() 

In Python 3, the newer {..} notation is used when echoing the object, unless it is empty:

>>> {'foo'} {'foo'} >>> _ - {'foo'}  # difference, removing the one element set() 

The set() type was added to the Python language in version 2.4 (see PEP 218), the curly brace syntax for set literals was added in Python 3 and back-ported to Python 2.7.

like image 39
Martijn Pieters Avatar answered Oct 13 '22 11:10

Martijn Pieters