Dictionaries and Tuples in Python
In this tutorial we shall discuss about the dictionaries and tuples in python. In the previous tutorial we have discussed:
- What is List?
- Functions that can be useful in Lists.
In this tutorial, you will come to know clearly about Dictionaries and Tuples. In general, the dictionaries are a type of data structure that has key values and many and Tuples is also a data structure that is similar to lists.
Dictionaries:
A dictionary is a data structure that has key values and some mapped values to each of the keys. It is also known as hashes. These keys and mapped values could be any data type in python.
E.g: my_dictionary= { ‘key1’:1, ‘key2’:2, ‘key3’:3}
To access the elements, We have to give key value as argument to the dictionary name. my_dictionary[0] #giving such values will give us an error
my_dictionary[‘key1’] #value respect to key1 will be printed
Updating a dictionary:
As we know that dictionaries are mutable and we can add, remove or interchange the values.
Operations can be performed on dictionaries:
Some of the functions were discussed in the previous article, click here to go there fromkeys(): This is a function to create a new dictionary using sequences, lists or other data structures.
get(): This function is equivalent to accessing a value using label indexing. My_dict[‘name’] == My_dict.get(‘name’) This function doesn’t give an error when an undefined key is given as an argument.
items(): This function gives all items (keys and values ) in a clean arranged way.
update(): This function is to add items in a dictionary from another dictionary.
values(): This function is to display all values of a dictionary.
Lists in a dictionary:
A list can be assign as a value in a dictionary. my_dict={‘students’:[‘vineet’,’harsh’,’vijay’],’marks’:[34,45,75]}
Tuples:
A tuple is a data structure similar to lists, but it is immutable, it values cannot be changed. Tuples in python use parenthesis where a list uses square brackets. E.g: my_tuple = (‘a’, ‘b’, 5)
Basic operations on tuples:
- len(my_tuple) # Returns the number of elements in the tuple
- max(my_tuple) # Returns the maximum value of tuple, only when the elements of the tuple are of the same data type
- min (my_tuple) # Returns the minimum value of tuple, only when the elements of the tuple are of the same data type
- 3*(my_tuple) # triples all the elements present in the tuple
- tuple(seq) # converts a list into the tuple
important: Format is a very important function, where it identifies {} and fills them according to given arguments.
Set: It is a data structure in python which holds only unique values. A set is defined between curly brackets
E.g: my_set= { 4 ,35 , 53, 353,66, 35 ,23 ,35 } As the definition of a set says that it can hold only unique values but we have given multiple same values. Let us see what output we get
Multiple values are removed and only one value is kept in the set. set(): This is known as the set function which converts a list, sequence, or other forms of collected data into a set form of data.
Few Operational functions used for sets.
Add(): Add an element to the set.
intersection(): This function finds the intersections of two sets.
issubset(): This function takes an argument of another set and checks whether it is a subset of this set or not.
isdisjoint(): This function says whether the two sets have any common elements or not.
If the output comes to be true, it means both the sets have null intersection. What is True and False ,will be discussing in upcoming tutorial.
This is the end of this tutorial about dictionaries and tuples in python. For more information visit the section of Data Science.