Hello folks.....!!!!
Today we will see the data structure 'Dictionary' in python.
For integers you need not use quotes and for char/string you have to use either single or double quotes.
Today we will see the data structure 'Dictionary' in python.
Dictionary :
Dictionaries are nothing but key-value pairs. Imagine the original
dictionary, where there will be a word and its meaning will be given. In
the same way there will be one key and a value will be associated with
it. In Lists you will access an element with its index number, here you
would access an item with its key.
A List- positive and negative index are present |
A Dictionary- where index changes to key |
How to declare a Dictionary ?
For lists, you declare them with square brackets. Here we have to use
curly braces '{ }'. To declare an empty dictionary, you have to write dict1={}. Once you declare an empty dictionary, you can initialize it with
key-value pairs. You can do it either with static values or with user
inputs. For example: Initializing statically
For integers you need not use quotes and for char/string you have to use either single or double quotes.
Assigning values to a dictionary
- Consider an empty dictionary dict1={}.
- To add elements or key-value pairs, the syntax is: dict_name[key]=value.
- Suppose that we have to add a pair as name=crazy girl, then we have to write as dict1['name']='crazy girl'.
- In the same way we can add any number of items. For integers we must not add quotes. For example: dict1['age']=23. Here, Note that the key is a string so we write it in quotes, but the value is an integer. So we don't write it in quotes.
- You can use either single or double quotes.
What will happen if you add the key already present in the dictionary with new values ?
It would assign the new value to that key. The syntax above first
checks whether the key is present, if it is not present then it adds the
key-value pair to the dictionary. Otherwise it updates the key's value.
For example :
Methods associated with dictionaries :
1.dict():
- This method is used to create a dictionary in variety of methods. It allows us to create dictionary using the other data structures.
- It takes only one argument.
- You can create a dictionary using the data structures Lists and Tuples (pronounced as ta-ple). For now, just remember a Tuple is a key value pair enclosed within '( )' and separated by comma ','.For example:
- You can also mix up and write as :
2. copy() :
- This method is used to copy the contents of one dictionary to other.
- The syntax is dict2=original_dict.copy( ).
- This creates a shallow copy, ie., changes to the copied one does not affect the original one.
- If you use '=' to assign the values of one dictionary to other, then it creates a deep copy where changes to the copied on reflect on the original one. For example:
- The Output is :
3. keys() :
- If you want to work only on the keys of a dictionary, then you can use this method.
- The syntax is dict_name.keys().
- It is used in several machine learning or data mining problems or even in some placement problems to iterate using the keys. But we will see them later. For now just know that it iterates over the keys in a dictionary without any loops. Example:
4. values() :
5. items():
- If you want to work with both the keys and values of a dictionary, then you can use this method.
- The syntax is dict_name.items().
6. clear():
- This clears all the items in a dictionary.
- If you have assigned one dictionary to another using '=', then clearing any one will clear both the dictionaries.
- This doesn't take any parameters. The syntax is dict_name.clear().
- Try using this method on any dictionary you create.
7. len():
- This gives the length of a dictionary.
- The syntax is len(dict_name).
8. get() :
- Instead of accessing an element in dictionary as dict_name[key], you can use this method.
- The advantage of this method is that, it does not shoe an error if the key you have given is not present in the dictionary. It just returns none.
- The syntax is dict_name.get(Key).
- Try this method to get value for a key that is not present.
Exercise:
Consider an employee's record :
name : Steve
age : 26
designation : Consultant
city : Bangalore
- Create a dictionary for this record and display it.
- Now change the city of Steve to Chennai.
- Try displaying the Country of Steve without generating error.
- Display the length of the dictionary.
Next Topic👉
Comments
Post a Comment