Dictionary and Tuples in Python
Dictionary and Tuples in Python
Hello Friends, I am Nirav. And in this tutorial I going to show you Dictionary and Tuples in Python..
So, Let's get Started
Dictionary
What is Dictionary ?
A dictionary is an associative array (also known as hashes). Any key of the dictionary is associated (or mapped) to a value. The values of a dictionary can be any Python data type. So dictionary are unordered key-value-pairs.
Why we use Dictionary ?
To, give key value to any instruction or value..
How to create a Dictionary ?
Creating a dictionary is as simple as placing items inside curly braces {} separated by comma.
An item has a key and the corresponding value expressed as a pair, key: value.
While values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique.
How to access element from Dictionary
While indexing is used with other container types to access values, dictionary uses keys. Key can be used either inside square brackets or with the
get()
method.
The difference while using
get()
is that it returns None
instead of KeyError
, if the key is not found.How to change or add elements in a dictionary?
Dictionary are mutable. We can add new items or change the value of existing items using assignment operator.
If the key is already present, value gets updated, else a new key: value pair is added to the dictionary.
How to delete or remove elements from a dictionary?
We can remove a particular item in a dictionary by using the method pop(). This method removes as item with the provided key and returns the value.
The method, popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary. All the items can be removed at once using the clear() method.
We can also use the del keyword to remove individual items or the entire dictionary itself.
Python Dictionary Methods
Methods that are available with dictionary are tabulated below. Some of them have already been used in the above examples.
Method | Description |
---|---|
clear() | Remove all items form the dictionary. |
copy() | Return a shallow copy of the dictionary. |
fromkeys(seq[, v]) | Return a new dictionary with keys from seq and value equal to v(defaults to None ). |
get(key[,d]) | Return the value of key. If key doesnot exit, return d (defaults to None ). |
items() | Return a new view of the dictionary's items (key, value). |
keys() | Return a new view of the dictionary's keys. |
pop(key[,d]) | Remove the item with key and return its value or d if key is not found. If d is not provided and key is not found, raises KeyError . |
popitem() | Remove and return an arbitary item (key, value). Raises KeyError if the dictionary is empty. |
setdefault(key[,d]) | If key is in the dictionary, return its value. If not, insert key with a value of d and return d (defaults to None ). |
update([other]) | Update the dictionary with the key/value pairs from other, overwriting existing keys. |
values() | Return a new view of the dictionary's values |
Tuple
What is Tuple ?
Tuple is a list of values grouped to gather.
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
Advantages of Tuple over List
Since, tuples are quite similiar to lists, both of them are used in similar situations as well.
However, there are certain advantages of implementing a tuple over a list. Below listed are some of the main advantages:
- We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
- Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.
- Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
- If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
Creating a Tuple
A tuple is created by placing all the items (elements) inside a parentheses (), separated by comma. The parentheses are optional but is a good practice to write it.
A tuple can have any number of items and they may be of different types (integer, float, list, string etc.).
Creating a tuple with one element is a bit tricky.
Having one element within parentheses is not enough. We will need a trailing comma to indicate that it is in fact a tuple.
Accessing Elements in a Tuple
We can use the index operator [] to access an item in a tuple where the index starts from 0.
So, a tuple having 6 elements will have index from 0 to 5. Trying to access an element other that (6, 7,...) will raise an Index Error.
The index must be an integer, so we cannot use float or other types. This will result into Type Error.
Likewise, nested tuple are accessed using nested indexing, as shown in the example below.
We can access a range of items in a tuple by using the slicing operator - colon ":".
Changing a Tuple
Unlike lists, tuples are immutable.
This means that elements of a tuple cannot be changed once it has been assigned. But, if the element is itself a mutable datatype like list, its nested items can be changed.
We can also assign a tuple to different values (reassignment).
Deleting a Tuple
As discussed above, we cannot change the elements in a tuple. That also means we cannot delete or remove items from a tuple.
But deleting a tuple entirely is possible using the keyword del.
You also visit my you tube video regarding to this Topic
https://youtu.be/e1f3RoBhuto
Comments
Post a Comment