<jupyter_start><jupyter_text>Dict Creation<jupyter_code># empty dict 
my_dict = {}

#dictionary with integer keys
my_dict = {1: 'abc', 2: 'xyz'}
print(my_dict)

#dictionary with mixed keys
my_dict = {'name': 'Pramit', 1: ['abc', 'xyz']}
print(my_dict)


#create empty dictionary using dict()
my_dict = dict()

my_dict = dict([(1, 'abc'), (2, 'xyz')])    #create a dict with list of tuples
print(my_dict)<jupyter_output>{1: 'abc', 2: 'xyz'}
{'name': 'Pramit', 1: ['abc', 'xyz']}
{1: 'abc', 2: 'xyz'}<jupyter_text>Dict Access<jupyter_code>my_dict = {'name':'Pramit', 'age':20,'address': 'kolkata'}

#get name
print(my_dict['name'])
print(my_dict['degree'])
# another way of accessing
print(my_dict.get("address"))
print(my_dict.get('degree'))<jupyter_output>None<jupyter_text>Dict Add or Modify Elements<jupyter_code>my_dict = {'name':'pramit','age':20,'address':'kolkata'}

# update name
my_dict['name'] = 'Pramit'

print(my_dict)
# adding new key
my_dict['degree'] = 'Bachelors of Pharmaceutical Technology'

print(my_dict)<jupyter_output>{'name': 'Pramit', 'age': 20, 'address': 'kolkata', 'degree': 'Bachelors of Pharmaceutical Technology'}<jupyter_text>Dict Delete or Remove Element<jupyter_code>my_dict = {'name':'pramit','age':20,'address':'kolkata'}

# remove a particular item
print(my_dict.pop('age'))

print(my_dict)
my_dict = {'name':'pramit','age':20,'address':'kolkata'}

# remove an arbitary item
my_dict.popitem()

print(my_dict)
squares = {2: 4, 3: 9, 4: 16, 5: 25}

# delete particular key

del squares[2]

print(squares)
squares.clear()

print(squares)
squares = {2: 4, 3: 9, 4: 16, 5: 25}

# delete the dictionary
del squares

print(squares)<jupyter_output><empty_output><jupyter_text>Dictionary Methods<jupyter_code>squares = {2: 4, 3: 9, 4: 16, 5: 25}

my_dict = squares.copy()
print(my_dict)
# fromkeys

subjects = {}.fromkeys(['Math','English','Bengali'],0)
print(subjects)
subjects = {2: 4, 3: 9, 4: 16, 5: 25}
print(subjects.items())  # returns a new view of the dictionary 
subjects = {2: 4, 3: 9, 4: 16, 5: 25}
print(subjects.keys())
print(subjects.values())
#get list of all available methods and attributes of dictionary
d = {}
print(dir(d))<jupyter_output>['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']<jupyter_text>Dict Comprehension<jupyter_code>d = {'a': 1, 'b': 2, 'c': 3}
for pair in d.items():
    print(pair)
#Creating a new dictionary with only pairs where the value is larger than 2
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

new_dict = {k:v for k,v in d.items() if v > 2}
print(new_dict)
#We can also perform operations on the key value pairs
d = {'a':1,'b':2,'c':3,'d':4,'e':5}
d = {k+'c':v*2 for k,v in d.items() if v > 2}
print(d)<jupyter_output>{'cc': 6, 'dc': 8, 'ec': 10}
{'fraction_non_alphanumeric': 0.19949574535140246, 'fraction_numerical': 0.02899464229435865, 'mean_word_length': 6.895522388059701, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '215602', 'license': 'MIT', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1260, 'n_words': 320, 'path': 'Machine Learning and Artificial Intelligence/01. Fundamentals-of-Programming/02.Python-for-Data-Science_Data Structures/05.Dictionary.ipynb', 'repo_name': 'pramit026/Machine-Learning-and-Artificial-Intelligence', 'size': '3173'}