Quick actions

cmd+k|ctrl+k

Navigation

Languages

tuple key

Snippet info

Language

Python

Visibility

public

Author

chausage

Created

2024-12-04T08:30:20.765749Z

Updated

2024-12-04T08:54:12.658967Z

#Example of using tuples as dictionary keys
location_data={  #dictionary
    ('NY', 'USA'): 1000,
    ('LA', 'USA'):2000,
    ('TO', 'Canada'):3000
}

#Accessing data using a tuple key
print(location_data[('NY', 'USA')]) #Output:1000

#Adding a new entry
location_data[('CG','USA')]=26000

#Iterating over the dictionary
for location, population in location_data.items():
    city, country=location
    print(f"The population of {city} in {country} is {population}.")
def a():
    print('apple')
    return('CG','USA')
location_data[a]=10
print(location_data[a()])
    
    
INFO