Category: Tutorials

  • Access Array Items

    Accessing an array items in Python refers to the process of retrieving the value stored at a specific index in the given array. Here, index is an numerical value that indicates the location of array items. Thus, you can use this index to access elements of an array in Python. An array is a container…

  • Arrays

    Arrays in Python Unlike other programming languages like C++ or Java, Python does not have built-in support for arrays. However, Python has several data types like lists and tuples (especially lists) that are often used as arrays but, items stored in these types of sequences need not be of the same type. In addition, we…

  • Dictionary Exercises

    Dictionary Exercise 1 Python program to create a new dictionary by extracting the keys from a given dictionary. Open Compiler It will produce the following output − Dictionary Exercise 2 Python program to convert a dictionary to list of (k,v) tuples. Open Compiler It will produce the following output − Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified…

  • Dictionary Methods

    A Python dictionary is an object of the built-in dict class, which defines the following methods − Dictionary Methods Sr.No. Method and Description 1 dict.clear()Removes all elements of dictionary dict. 2 dict.copy()Returns a shallow copy of dictionary dict. 3 dict.fromkeys()Create a new dictionary with keys from seq and values set to value. 4 dict.get(key, default=None)For key key, returns value…

  •  Nested Dictionaries

    Nested Dictionaries Nested dictionaries in Python refer to dictionaries that are stored as values within another dictionary. In other words, a dictionary can contain other dictionaries as its values, forming a hierarchical or nested structure. Nested dictionaries can be modified, updated, or extended in the same way as regular dictionaries. You can add, remove, or update…

  • Copy Dictionaries

    Copy Dictionaries Copying dictionaries in Python refers to creating a new dictionary that contains the same key-value pairs as the original dictionary. We can copy dictionaries using various ways, depending on the requirements and the nature of the dictionary’s values (whether they are mutable or immutable, nested or not). Shallow Copy When you perform a…

  • Loop Dictionaries

    Loop Through Dictionaries Looping through dictionaries in Python refers to iterating over key-value pairs within the dictionary and performing operations on each pair. This allows you to access both keys and their corresponding values. There are several ways/methods for looping through dictionaries − Loop Through Dictionary Using a For Loop A for loop in Python…

  • Dictionary View Objects

    The items(), keys(), and values() methods of dict class return view objects. These views are refreshed dynamically whenever any change occurs in the contents of their source dictionary object. The items() Method The items() method returns a dict_items view object. It contains a list of tuples, each tuple made up of respective key, value pairs. Syntax Following is the syntax of the items() method − Return…

  • Remove Dictionary Items

    Remove Dictionary Items Removing dictionary items in Python refers to deleting key-value pairs from an existing dictionary. Dictionaries are mutable data structures that hold pairs of keys and their associated values. Each key acts as a unique identifier, mapping to a specific value within the dictionary. Removing items from a dictionary allows you to eliminate…

  • Add Dictionary Items

    Add Dictionary Items Adding dictionary items in Python refers to inserting new key-value pairs into an existing dictionary. Dictionaries are mutable data structures that store collections of key-value pairs, where each key is associated with a corresponding value. Adding items to a dictionary allows you to dynamically update and expand its contents as needed during…