Thursday, January 14, 2021

Python Directories

 Python Dictionaries are storage location and values are stored in key:value pair. It cannot have duplicate key and if we insert duplicate key, it will overwrite the existing one.

mdict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

A dictionary is a collection which is unordered, changeable and does not allow duplicates.

mdict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

print(mdict) will display all key and values.

Dictionary items are presented in key:value pairs, and can be referred to by using the key name.

Example

Print the "brand" value of the dictionary:

mdict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

print(mdict["brand"])


Duplicates Not Allowed

Dictionaries cannot have two items with the same key:

Duplicate values will overwrite existing values:

mdict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964,

  "year": 2020

}

"year": 2020 will overwrite "year": 1964


Accessing Directory Items

We can access the items of a dictionary by referring to its key name, inside square brackets:


Example

Get the value of the "India" key:


mdict = {

  "India":"Delhi",

  "Bangladesh": "Dhaka",

  "Sri Lanka": "Colombo"

}

x = mdict["India"]

There is also a method called get() that will give you the same result:


Example

Get the value of the "India" key:


x = mdict.get("India")

Get Keys

The keys() method will return a list of all the keys in the dictionary.

x = dict.keys()

The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.


Add a new item to the original dictionary.

car = {

"India":"Delhi",

"Bangladesh": "Dhaka",

"Sri Lanka": "Colombo"

}

x = car.keys()


print(x) #before the change

car["China"] = "Beijing" # new key value pair added in the directory


Change Values in Directory

There are two techniques to change value in directory.

We can change the value of a specific item by referring to its key name:

Change the "Key1" to Value:

mdict = {

  "Key1":"Value1",

 "Key2":"Value2",

  "Key3":"Value3"

}

mdict["year"] = Value


Using update() method, we can change value and this method takes the new value along with key as argument.

Update the "Key3" of the car by using the update() method:

mdict = {

  "Key1":"Value1",

 "Key2":"Value2",

  "Key3":"Value3"

}

mdict.update({"Key3": Value})


Add Dictionary Items


This is same as updating directory. Using a new index key, we can assign value to add elements in directory. If the 'key' is existing, the value at that key will be updated.

Example

mdict = {

  "Key1":"Value1",

  "Key2":"Value2",

  "Key3":"Value3"

}

mdict["Key"] = "Value"


The update() method will update the dictionary with the items from a given argument. If the item does not exist, the item will be added.


The argument must be a dictionary, or an iterable object with key:value pairs.


Example

Add a color item to the dictionary by using the update() method:


mdict = {

  "Key1":"Value1",

  "Key2":"Value2",

  "Key3":"Value3"

}

mdict.update({"Key": "Value"})


Remove Dictionary Items


We can remove an item from directory using the pop () method. This method takes the 'key' as argument and remove the item.

Example

dict = {

  "Key1":"Value1",

  "Key2":"Value2",

  "Key3":"Value3"

}

mdict.pop("Key1")

del statement can remove an item with specified key like

del mdict["Key1"] # It will remove the item associated with the key "Key1"

To delete entire directory use: del mdict

To empty a directory, clear () method is called on the directory like : mdict.clear()


The popitem() method will remove the last inserted element. In versions before 3.7, it will remove any random item

mdict = {

  "Key1":"Value1",

  "Key2":"Value2",

  "Key3":"Value3"

}

mdict.popitem()


Copy Directories

copy () method can copy a directory into a new one and return it.

Example

mdict = {

  "Key1":"Value1",

  "Key2":"Value2",

  "Key3":"Value3"

}

mndict=dict.copy()


dict() function can copy an existing directory into another

mndict=dict(mdict)


Nested Directories

Nested directory means directory under directory

myfamily = {
  "child1" : {
    "name" : "Sata",
    "year" : 1997
  },
  "child2" : {
    "name" : "Suddha",
    "year" : 2004
  }
}

Similarly we can create directories separately and put them inside another directory

child1 = {
    "name" : "Sata",
    "year" : 1997
  }
  child2 = {
    "name" : "Suddha",
    "year" : 2004
  }

myfamily = {
  "child1" : child1,
  "child2" : child2
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner