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.
dictGroup of heterogeneous elements
Each element is a key value pair.
All the keys are unique in the
dict.dictcan 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
dictcan be initialized using{}ordict().
tupleGroup of heterogeneous elements.
We can access the elements in
tupleonly by positional notation (by using index)tuplecan be created by enclosing elements in()- example(1, 2, 3, 4).
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0} # dict
d
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
type(d)
dict
d = dict() # Initializing empty dict
d
{}
d = {} # d will be of type dict
type(d)
dict
t = (1, 'Scott', 'Tiger', 1000.0) # tuple
type(t)
tuple
t
(1, 'Scott', 'Tiger', 1000.0)
t = ()
t
()
type(t)
tuple
t = tuple()
t
()