Dictionary Methods: Len, Str, Clear, Copy, Get, Update, Copy. Difference Between List and Dictionary

A dictionary provides several built in methods for managing and manipulating key-value pairs efficiently.

1. len()

The len() function returns the total number of key-value pairs in a dictionary.

Syntax

len(dictionary)

Example

student = {
    "name": "Kaif",
    "age": 20,
    "course": "Python"
}

print(len(student))

Output

3

Uses

  1. Counting dictionary items.
  2. Controlling loops.
  3. Checking dictionary size.

2. str()

The str() function converts a dictionary into its string representation.

Syntax

str(dictionary)

Example

student = {
    "name": "Kaif",
    "age": 20
}

print(str(student))

Output

"{'name': 'Kaif', 'age': 20}"

Uses

  1. Converting dictionary to string.
  2. Displaying dictionary data.
  3. Logging and debugging.

3. clear()

The clear() method removes all items from a dictionary.

Syntax

dictionary.clear()

Example

student = {
    "name": "Kaif",
    "age": 20
}

student.clear()

print(student)

Output

{}

Characteristics

  1. Deletes all key-value pairs.
  2. Dictionary remains but becomes empty.

4. copy()

The copy() method creates a shallow copy of a dictionary.

Syntax

dictionary.copy()

Example

student = {
    "name": "Kaif",
    "age": 20
}

new_student = student.copy()

print(new_student)

Output

{'name': 'Kaif', 'age': 20}

Characteristics

  1. Creates a separate dictionary.
  2. Changes in one dictionary do not affect the other.

5. get()

The get() method returns the value associated with a specified key.

Syntax

dictionary.get(key)

Example

student = {
    "name": "Kaif",
    "age": 20
}

print(student.get("name"))

Output

Kaif

If Key Does Not Exist

print(student.get("city"))

Output

None

Advantages

  1. Prevents KeyError.
  2. Safer than direct key access.
  3. Returns None if key is absent.

6. update()

The update() method adds new items or modifies existing items.

Syntax

dictionary.update(other_dictionary)

Example 1: Adding Items

student = {
    "name": "Kaif",
    "age": 20
}

student.update({"city": "Mumbai"})

print(student)

Output

{'name': 'Kaif', 'age': 20, 'city': 'Mumbai'}

Example 2: Modifying Items

student = {
    "name": "Kaif",
    "age": 20
}

student.update({"age": 21})

print(student)

Output

{'name': 'Kaif', 'age': 21}

Characteristics

  1. Adds new key-value pairs.
  2. Updates existing values.
  3. Modifies the original dictionary.

Summary Table

Method Purpose
len() Returns number of key-value pairs
str() Converts dictionary to string
clear() Removes all items
copy() Creates a copy of dictionary
get() Retrieves value using key
update() Adds or modifies items

Difference Between List and Dictionary:

Basis List Dictionary
Definition Collection of elements Collection of key-value pairs
Symbol [ ] { }
Access Method Index Key
Ordering Ordered Ordered (Python 3.7+)
Modification Mutable Mutable
Duplicate Values Allowed Values allowed, keys unique
Searching By index By key
Storage Format Single values Key-value pairs
Example [10, 20, 30] {“a”:10, “b”:20}
Retrieval Speed Slower for searching Faster using keys

Example of List

fruits = ["Apple", "Mango", "Orange"]

print(fruits[1])

Output

Mango

Example of Dictionary

student = {
    "name": "Kaif",
    "age": 20
}

print(student["name"])

Output

Kaif

Leave a Reply

error: Content is protected !!