AI Basics with AK

Season 02 - Introduction to Python Programming

Arun Koundinya Parasa

Episode 10

DICTIONARIES

Dictionary and Dicitonary Operations

  • Dictionary
  • Dictionary Operations
  • Dictionary Comprehenions

Dictionary


my_dict = {
  "name": "Arun Koundinya",
  "age": 37,
  "birthcountry": "India"
}

print(my_dict)
{'name': 'Arun Koundinya', 'age': 37, 'birthcountry': 'India'}
type(my_dict)
dict
my_dict[0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-2e042f7087f7> in <cell line: 1>()
----> 1 my_dict[0]

KeyError: 0
my_dict['age']
37
my_dict["love"] = "life"

print(my_dict)
{'name': 'Arun Koundinya', 'age': 37, 'birthcountry': 'India', 'love': 'life'}
my_dict =  {
    0 : "Zero",
    1 : "One"
}
my_dict[0]
'Zero'
dict(name="Arun",age="37")
{'name': 'Arun', 'age': '37'}
dict(0="Zero",1="One")
  File "<ipython-input-20-2b41aa938669>", line 1
    dict(0="Zero",1="One")
         ^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Dictionary Type Casting

my_list = [1,2]
dict(my_list)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-62e4795c3a60> in <cell line: 2>()
      1 my_list = [1,2]
----> 2 dict(my_list)

TypeError: cannot convert dictionary update sequence element #0 to a sequence
my_list = [[1,2],[2,3],[3,4]]
dict(my_list)
{1: 2, 2: 3, 3: 4}
my_list = [[1,2],[1,2],[1,2]]
dict(my_list)
{1: 2}
my_list = [[1,2],[1,3],[1,4]]
dict(my_list)
{1: 4}

Dictionary Operations

dummy_dict = {
    'apple': 5,
    'banana': 7,
    'orange': 3,
    'kiwi': 10
}
print('apple' in dummy_dict)
True
print('arun' in dummy_dict)
False
len(dummy_dict)
4
print(dummy_dict.keys())
dict_keys(['apple', 'banana', 'orange', 'kiwi'])
print(dummy_dict.values())
dict_values([5, 7, 3, 10])
print(dummy_dict.items())
dict_items([('apple', 5), ('banana', 7), ('orange', 3), ('kiwi', 10)])
type(dummy_dict.items())
dict_items
for key,value in dummy_dict.items():
  print(key)
  print(value)
apple
5
banana
7
orange
3
kiwi
10
for key,value in dummy_dict.items():
  print("key:",key," value:",value)
key: apple  value: 5
key: banana  value: 7
key: orange  value: 3
key: kiwi  value: 10
dummy_dict
{'apple': 5, 'banana': 7, 'orange': 3, 'kiwi': 10}
dummy_dict["blackberries"] = 50
print(dummy_dict)
{'apple': 5, 'banana': 7, 'orange': 3, 'kiwi': 10, 'blackberries': 50}

Dictionary Operations and Comprehensions

new_dict = {
    "blueberries" : 50,
    "grapes" : 500,
    "watermelon" : 1
    }

new_dict
{'blueberries': 50, 'grapes': 500, 'watermelon': 1}
dummy_dict + new_dict
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-5159f89235ff> in <cell line: 1>()
----> 1 dummy_dict + new_dict

TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
dummy_dict.update(new_dict)
print(dummy_dict)
{'apple': 5, 'banana': 7, 'orange': 3, 'kiwi': 10, 'blackberries': 50, 'blueberries': 50, 'grapes': 500, 'watermelon': 1}
dummy_dict.get("watermelon")
1
dummy_dict.clear()
dummy_dict
{}
sentence = "The cat lounged lazily on the sunlit windowsill, purring contentedly"
letter_focus = 'aeiou'
print(sentence.count('a'))
print(sentence.count('e'))
print(sentence.count('i'))
print(sentence.count('o'))
print(sentence.count('u'))
2
5
5
4
3
for letters in letter_focus:
  print(letters, sentence.count(letters))
a 2
e 5
i 5
o 4
u 3
{letter: sentence.count(letter) for letter in letter_focus}
{'a': 2, 'e': 5, 'i': 5, 'o': 4, 'u': 3}
sentence = "The cat lounged lazily on the sunlit windowsill, purring contentedly"
letter_focus = 'aeioub'
{letter: sentence.count(letter) for letter in letter_focus}
{'a': 2, 'e': 5, 'i': 5, 'o': 4, 'u': 3, 'b': 0}

Thank You

Open In Colab