Thursday 27 June 2024

7 level of writing Python Dictionary

Level 1: Basic Dictionary Creation
Create a simple dictionary with key-value pairs.

# Creating a basic dictionary
person = {
    "name": "Alice",
    "age": 30,
    "city": "New York"
}
print(person)

#Clcoding.com
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Level 2: Accessing and Modifying values

Level 2: Accessing and Modifying Values Access values using keys, and modify existing key-value pairs. # Accessing values print(person["name"]) # Modifying values person["age"] = 31 print(person["age"]) #Clcoding.com Alice 31


Level 3: Adding and Removing key Values Pairs

Level 3: Adding and Removing Key-Value Pairs Add new key-value pairs and remove existing ones. # Adding a new key-value pair person["email"] = "alice@example.com" print(person) # Removing a key-value pair del person["city"] print(person) #Clcoding.com {'name': 'Alice', 'age': 31, 'city': 'New York', 'email': 'alice@example.com'} {'name': 'Alice', 'age': 31, 'email': 'alice@example.com'}
Level 4: Dictionary Methods

Level 4: Dictionary Methods Use dictionary methods like keys(), values(), items(), get(), and pop() # Getting all keys print(person.keys()) # Getting all values print(person.values()) # Getting all key-value pairs print(person.items()) # Using get() method print(person.get("name")) print(person.get("city", "Not Found")) # Using pop() method email = person.pop("email") print(email) print(person) dict_keys(['name', 'age', 'email']) dict_values(['Alice', 31, 'alice@example.com']) dict_items([('name', 'Alice'), ('age', 31), ('email', 'alice@example.com')]) Alice Not Found alice@example.com {'name': 'Alice', 'age': 31}

Level 5: Dictionary Comprehensions
Level 5: Dictionary Comprehensions
Create dictionaries using dictionary comprehensions for more concise and readable code.

# Dictionary comprehension
squares = {x: x*x for x in range(6)}
print(squares)

#Clcoding.com
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Level 6: Nested Dictionary
Level 6: Nested Dictionaries
Work with dictionaries within dictionaries to represent more complex data structures.

# Nested dictionary
people = {
    "person1": {
        "name": "Alice",
        "age": 30
    },
    "person2": {
        "name": "Bob",
        "age": 25
    }
}
print(people)

# Accessing nested dictionary values
print(people["person1"]["name"])  

#Clcoding.com
{'person1': {'name': 'Alice', 'age': 30}, 'person2': {'name': 'Bob', 'age': 25}}
Alice

Level 7: Advanced Dictionary Operations

Level 7: Advanced Dictionary Operations Using advanced features like merging dictionaries, using defaultdict from collections, and performing operations with dict and zip # Merging dictionaries (Python 3.9+) dict1 = {"a": 1, "b": 2} dict2 = {"b": 3, "c": 4} merged_dict = dict1 | dict2 print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4} # Using defaultdict from collections import defaultdict dd = defaultdict(int) dd["key1"] += 1 print(dd) # Output: defaultdict(<class 'int'>, {'key1': 1}) # Creating a dictionary from two lists using zip keys = ["name", "age", "city"] values = ["Charlie", 28, "Los Angeles"] person = dict(zip(keys, values)) print(person) #Clcoding.com {'a': 1, 'b': 3, 'c': 4} defaultdict(<class 'int'>, {'key1': 1}) {'name': 'Charlie', 'age': 28, 'city': 'Los Angeles'}

0 Comments:

Post a Comment

Popular Posts

Categories

AI (28) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (121) C (77) C# (12) C++ (82) Course (64) Coursera (182) Cybersecurity (24) data management (11) Data Science (99) Data Strucures (6) Deep Learning (11) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Pandas (3) PHP (20) Projects (29) Python (777) Python Coding Challenge (260) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses