A hash table, also known as a hash map, is a data structure that is used to store key-value pairs. It is an efficient way to store data that can be quickly retrieved using a unique key.
Here is an example of how to declare and write a script with a hash table in Python:
# Declare an empty hash table
# Add some key-value pairs to the hash table
hash_table[‘key1’] = ‘value1’
hash_table[‘key2’] = ‘value2’
hash_table[‘key3’] = ‘value3’
# Access a value using its key
print(hash_table[‘key2’]) # Output: “value2”
# Modify a value using its key
hash_table[‘key2’] = ‘new value’
print(hash_table[‘key2’]) # Output: “new value”
# Delete a key-value pair using the `del` statement
del hash_table[‘key1’]
# Check if a key is in the hash table using the `in` operator
print(‘key1’ in hash_table) # Output: False
# Output: False
In this example, we declare an empty hash table using the {}
syntax. We then add some key-value pairs to the hash table using the []
syntax. We access a value using its key, modify a value using its key, delete a key-value pair using the del
statement, and check if a key is in the hash table using the in
operator.
I hope this helps! Let me know if you have any questions.
Declaration:
$states=@{“Alberta” = “Calgary”; “British Columbia” = “Vancouver”; “Ontario” = “Toronto” ; “Quebec” = “Montreal”}
Name _____ | Value _______ |
Alberta | Calgary |
British Columbia | Vancouver |
Ontario | Toronto |
Quebec | Montreal |
Add new key-value in hashtable:
$states.Add(“Manitoba”,”Winnipeg”)
Remove key-value in hashtable:
$states.Remove(“Manitoba”,”Winnipeg”)
Change value in hashtable:
$states.Set_Item(“Ontario”,”Ottawa”)
Retrieve value in hashtable:
$states.Get_Item(“Alberta”)
Find key in hashtable:
$states.ContainsKey(“Alberta”)
Find Value in hashtable:
$states.ContainsValue(“Calgary”)
Count items in hashtable:
$states.Count
Sort items by Name in hashtable:
$states.GetEnumerator() | Sort-Object Name -descending
Sort items by Value in hashtable:
$states.GetEnumerator() | Sort-Object Value -descending
Declaration:
my %hash = (); #Initialize a hash
my $hash_ref = {}; # Initialize a hash reference. ref will return HASH
Clear (or empty) a hash
for (keys %hash)
{
delete $hash{$_};
}
Clear (or empty) a hash reference
for (keys %$href)
{
delete $href->{$_};
}
Add a key/value pair to a hash
$hash{ ‘key’ } = ‘value’; # hash
$hash{ $key } = $value; # hash, using variables
Using Hash Reference
$href->{ ‘key’ } = ‘value’; # hash ref
$href->{ $key } = $value; # hash ref, using variables
Add several key/value pairs to a hash
%hash = ( ‘key1’, ‘value1’, ‘key2’, ‘value2’, ‘key3’, ‘value3’ );
%hash = (
key1 => ‘value1’,
key2 => ‘value2’,
key3 => ‘value3’,
);
Copy a hash
my %hash_copy = %hash; # copy a hash
my $href_copy = $href; # copy a hash ref
Delete a single key/value pair
delete $hash{$key};
delete $hash_ref->{$key};
Hash tables are called dictionary in python.
Declaration:
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
Accessing Values
print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]
Output:
dict[‘Name’]: Zara
dict[‘Age’]: 7
Updating Dictionary
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
dict[‘Age’] = 8; # update existing entry
dict[‘School’] = “DPS School”; # Add new entry
Delete Dictionary Elements
#!/usr/bin/python
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
del dict[‘Name’]; # remove entry with key ‘Name’
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
SQL Interview Questions and Answers In the world of data-driven decision-making, SQL (Structured Query Language)…
Before you make the decision to switch your home’s interest service provider, take the time…
AI Innovations in April 2024. Welcome to the April 2024 edition of the Daily Chronicle,…
A bioreactor is the silent hero behind the scenes contributing to many of everyday life’s…
Don’t let a few obstacles get in the way of success. Learn how to overcome…
A power strip and a surge protector might seem like the same device, but there…