Python Dictionaries

Python Dictionaries

This article is related to all functions present inside python dictionary function

Table of contents

No heading

No headings in the article.

  1. clear()

    • Removes all the elements from the dictionary

      car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
      }
      car.clear()
      print(car)
      
  2. copy()

    • Returns a copy of the dictionary

      car = {
          "brand": "Mercedes",
          "model": "AMG",
          "year": 2001
      }
      x = car.copy()
      print(x)
      
  3. fromkeys()

    • Returns a dictionary with the specified keys and value

      x = ('car', 'company', 'fruit')
      y = 'apple'
      thisdict = dict.fromkeys(x,y)
      print(thisdict)
      
  4. get()

    • Returns the value of the specified key

      car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
      }
      x = car.get("model")
      print(x)
      
  5. items()

    • Returns a list containing a tuple for each key value pair

      car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
      }
      x = car.items()
      print(x)
      
  6. keys()

    • Returns a list containing the dictionary’s keys

      car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
      }
      x = car.keys()
      print(x)
      
  7. pop()

    • Removes the element with the specified key

      car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
      }
      car.pop("model")
      print(car)
      
  8. popitem()

    • Removes the last inserted key-value pair

      car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
      }
      car.popitem()
      print(car)
      
  9. setdefault()

    • Returns the value of the specified key. If the key does not exist: insert the key, with the specified value

      car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
      }
      x = car.setdefault("model", "CLS")
      print(x)
      
  10. update()

    • Updates the dictionary with the specified key-value pairs
    car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
    }
    car.update({"color": "Black"})
    print(car)
    
  11. values()

    • Returns a list of all the values in the dictionary
    car = {
        "brand": "Mercedes",
        "model": "AMG",
        "year": 2001
    }
    x = car.values()
    print(x)
    

Did you find this article valuable?

Support BigSmoke's Blog by becoming a sponsor. Any amount is appreciated!