Overview of dict and tuple¶
As we have gone through details related to list
and set
, now let us get an overview of dict
and tuple
in Python.* dict
- Group of heterogeneous elements
- Each element is a key value pair.
- All the keys are unique in the
dict
. dict
can be created by enclosing elements in{}
. Key Value pair in each element are separated by:
– example{1: 'a', 2: 'b', 3: 'c', 4: 'd'}
- Empty
dict
can be initialized using{}
ordict()
.tuple
- Group of heterogeneous elements.
- We can access the elements in
tuple
only by positional notation (by using index) tuple
can be created by enclosing elements in()
– example(1, 2, 3, 4)
.
In [1]:
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0} # dict
In [2]:
d
Out[2]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
In [3]:
type(d)
Out[3]:
dict
In [4]:
d = dict() # Initializing empty dict
In [5]:
d
Out[5]:
{}
In [6]:
d = {} # d will be of type dict
In [7]:
type(d)
Out[7]:
dict
In [8]:
t = (1, 'Scott', 'Tiger', 1000.0) # tuple
In [9]:
type(t)
Out[9]:
tuple
In [10]:
t
Out[10]:
(1, 'Scott', 'Tiger', 1000.0)
In [11]:
t = ()
In [12]:
t
Out[12]:
()
In [13]:
type(t)
Out[13]:
tuple
In [14]:
t = tuple()
In [15]:
t
Out[15]:
()