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

Dictionaries Begining.png

Updating a dictionary:

As we know that dictionaries are mutable and we can add, remove or interchange the values.

Updating Dictionaries in python.png

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.

fromkeys.png

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.

get.png

items(): This function gives all items (keys and values ) in a clean arranged way.

items.png

update(): This function is to add items in a dictionary from another dictionary.

update.png

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]}

lists in dic.png

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
tuples in python.png

important: Format is a very important function, where it identifies {} and fills them according to given arguments.

important tuple in python.png

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

set tuple in python.png

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.

tuples in python

Few Operational  functions used for sets.

Add(): Add an element to the set.

Add.png

intersection(): This function finds the intersections of two sets.

Intersection sets.png

issubset(): This function takes an argument of another set and checks whether it is a subset of this set or not.

issubset.png

isdisjoint(): This function says whether the two sets have any common elements or not.

isdisjoint.png

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.

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *