Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Dictionary

►A dictionary is a collection of unordered, immutable, indexed having key value pair kind of elements enclosed by curly braces.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

►You can access an element of a dictionary by referring to its key name, inside square brackets.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

print(c[2])

►You can change the value of a specific element by referring to its key name:

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

c[1] : “ww”

print(c)

►You can traverse through dictionary using for loop

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

for x in c:

print(x)

♦Methods:

►get(): Used to get the value from the key in a dictionary.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

print(c.get(2)

►values(): Used to print all the values from the dictionary.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

for x in c.values()

print(x)

►items(): Used to traverse through dictionary to print both key and values together.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

for x,y in c.items():

print(c[2])

►len(): Used to determine total number of elements in dictionary.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

print(len(c))

►pop(): Used to delete a value with specified key in a dictionary.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

print(c.pop(2))

►popitem(): Used to delete last inserted element from dictionary.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

print(c.popitem())

►del(): Used to delete entire dictionary.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

c.del()

►copy(): Used to create copy of a dictionary.

Ex)    c = { 1: “aa”, 2: “bba”, 3: “ab”, 4: “ba”}

x=c.copy()

print(x)

The post Dictionary appeared first on Prwatech.



This post first appeared on Learn Big Data Hadoop In Bangalore, please read the originial post: here

Share the post

Dictionary

×

Subscribe to Learn Big Data Hadoop In Bangalore

Get updates delivered right to your inbox!

Thank you for your subscription

×