Jan 2, 2013

Dict Example in Python

Today, we discuss about the Dict concept in Python.

Dict in Python consists of Key & Value pairs. This is similar to Hash concept in Perl language.


Keys are unique in Dict, Values are not necessarily unique in nature.


Let's discuss with an example, let's dive & have fun :



relatives = {"Lisa" : "daughter", "Bart" : "son", "Marge" : "mother", "Homer" : "father", "Santa" : "dog"}


#raw_input(), print for Python 2.X Version 
#input(), print() for Python 3.X Version


#Keys are unique


relatives['Marge'] = "mother";

relatives['marge'] = "mother1";

#'Marge' and 'marge' are different keys


for member in sorted(relatives.keys()):

    print("\n",member, "=>", relatives[member])
#print "\n",member

for member in sorted(relatives.values()):

    print("\n",member)
#print "\n",member

for key, value in relatives.items():
print("\n", key, "=>", value)
#print "\n", key, "=>", value




Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


No comments:

Post a Comment