Saturday, 1 April 2017

collection data type: dict

dict

  • Dictionary can be created by using curly brackets {}or  by calling dict() function
  • Dictionary represents group of key-value pairs. Each key-value pair in the dictionary is known as an item.
  • Dictionary is a mutable object.
  • Keys must be immutable objects.
  • values can be mutable objects or immutable objects.
  • Insertion order is not preserved.
  • Duplicate keys are not allowed but values can be duplicate.
  • Heterogeneous key and values are allowed.

x={}
print(x)
print(type(x))

{ }
<class 'dict'>

y=dict()
print(y)
print(type(y))

{ }
<class 'dict'>

z={ 'JAVA':90,'hadoop':85,'python':89 }
print(z)
print(type(z))

{ 'python': 89, 'hadoop': 85, 'JAVA': 90 }
<class 'dict'>

a={ 'java':90,10:'hyd',True:1234,20:'hyd',30:[1,2,3],'java':95 }
print(a)

{ True: 1234, 'java': 95, 20: 'hyd', 10: 'hyd', 30: [1, 2, 3] }

b={'hyd':40,'bang':20,'Delhi':15}
print(b)
b['chennai']=42
print(b)

{ 'hyd': 40, 'bang': 20, 'Delhi': 15 }
{ 'chennai': 42, 'hyd': 40, 'bang': 20, 'Delhi': 15 }

print(b['hyd'])

40

c={'hyd':40,'bang':20,'Delhi':15}
print(c)
k=c.keys();
for p in k:
         print(p)
v=c.values();
for q in v:
         print(q)

{'bang': 20, 'Delhi': 15, 'hyd': 40}
bang
Delhi
hyd
20
15
40

Python Dictionary Methods


Python Dictionary Methods
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 keydoesnot exit, return d (defaults toNone).
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 andkey is not found, raisesKeyError.
popitem()
Remove and return an arbitary item (key, value). Raises KeyErrorif 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


d={'java':90,'python':98,'hadoop':85,'oracle':90}
print(d)
print(d['python'])
print(d.get('hadoop'))
d.pop('oracle')
print(d)
print(d.popitem())
print(d)
d.clear()
print(d)

{'java': 90, 'oracle': 90, 'python': 98, 'hadoop': 85}
98
85
{'java': 90, 'python': 98, 'hadoop': 85}
('java', 90)
{'python': 98, 'hadoop': 85}
{ }

marks={}
e=marks.fromkeys(['Math','English','Science'],10)
print(e)

{'Science': 10, 'English': 10, 'Math': 10}


marks={}
f=marks.fromkeys(['Math','English','Science'],[10,20,30])
print(f)

{'Science': [10, 20, 30], 'English': [10, 20, 30], 'Math': [10, 20, 30]}

Dictionary Comprehension:

x={p:p**2 for p in range(10)}
print(x)

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}


y={q:q**q for q in range(5)}
print(y)

{0: 1, 1: 1, 2: 4, 3: 27, 4: 256}

z={r:r*2 for r in range(10) if r%2==0}
print(z)

{0: 0, 8: 16, 2: 4, 4: 8, 6: 12}

No comments:

Post a Comment