Dictionary Operations
Posted: Mon May 27, 2024 9:36 am
Dictionary:
A dictionary in Python is a data structure that stores the value in value:key pairs. The most common associated method of dictionaries are get(), keys(), values(), items(), update(), pop(), clear() etc.
Example: Dict = {1: ‘ICAI', 2: 'For', 3: ‘CA’}
As you can see from the example, data is stored in key: value pairs in dictionaries, which makes it easier to find values.
Dictionary vs Json
#Update balance
#Looping through ledger
print(person['name']) # Output: John
[/code]
A dictionary in Python is a data structure that stores the value in value:key pairs. The most common associated method of dictionaries are get(), keys(), values(), items(), update(), pop(), clear() etc.
Example: Dict = {1: ‘ICAI', 2: 'For', 3: ‘CA’}
As you can see from the example, data is stored in key: value pairs in dictionaries, which makes it easier to find values.
Dictionary vs Json
Code: Select all
def get_coordinates():
x = 13.339
y = 80.1425
return x, y
coordinates = get_coordinates()
print(coordinatesCode: Select all
person = {'name': 'John', 'age': 25, 'city': 'New York'}
#Client Ledger (Name -> Balance)
ledger = {"Ramesh": 12000, "Suresh": -5000}
print("Ramesh's balance:", ledger["Ramesh"])Code: Select all
ledger["Suresh"] += 2000
print("Updated Suresh's balance:", ledger["Suresh"])Code: Select all
for client, balance in ledger.items():
print(client, "=>", balance)[/code]