Skip to main content

Dictionaries

A dictionary in EasyBite is a collection of key-value pairs, where each key maps to a corresponding value. Dictionaries are also referred to as associative arrays or hashmaps in some other programming languages.


Why Use Dictionaries?

Dictionaries are used when you need to store and retrieve data using a unique identifier (the key) rather than by index (as in lists). This is particularly useful in scenarios like:

  • Mapping data: For instance, storing a person's name and age, where the name is the key and the age is the value.
  • Efficient lookups: Finding values based on keys is very fast and efficient.
  • Flexibility: You can use any immutable type as the key (like strings, numbers, or tuples) and store any type of data as the value (strings, lists, etc.).

Declaring and Initializing a Dictionary

Dictionaries in EasyBite do not support declaration in the traditional sense. Instead, you initialize an empty dictionary by setting a variable to {}.

Example:

set person to {}
set person to {"name": "Ali", "age": 25, "city": "Lagos"}

Explanation:

  • The variable person is initialized as an empty dictionary using {}.
  • Later, it is populated with three key-value pairs: "name", "age", and "city", with corresponding values "Ali", 25, and "Lagos".

Accessing Dictionary Elements

You can access dictionary values by specifying the key inside square brackets [] or by using dot notation if the key is valid.

Example:

show(person["name"])    // "Ali"
show(person.age) // 25

Explanation:

  • person["name"] accesses the value associated with the key "name".
  • person.age uses dot notation to access the value associated with the key "age". Dot notation is supported for keys that are valid identifiers (such as letters, numbers, or underscores).

Three Ways to Access Dictionary Functions

Dictionaries in EasyBite support three methods to access their functions. These methods provide flexibility depending on the situation and your coding preferences.

Access MethodExample SyntaxDescription
1. Using Dot Notationperson.containskey("name")Accessing dictionary functions using dot notation, which works for methods that are part of the dictionary object (e.g., containskey).
2. Using Module Accessdict.containskey(person, "name")Using the dictionary module (e.g., dict.containskey) after importing the module.
3. Using Function Importimport containskey from dict
containskey(person, "name")
Importing specific dictionary functions for use (like containskey or values) for more granular control.

1. Using Dot Notation

You can access dictionary functions directly using dot notation, similar to how you access properties of an object. This works for methods that belong to the dictionary object.

Example:

set hasKey to person.containskey("name")  // Checks if the key "name" exists in the dictionary.

Explanation:

  • The containskey function checks if the dictionary contains the specified key. Here, we are using dot notation to access the function directly on the person dictionary.

2. Using Module Access

In this method, you import the dict module and access dictionary functions using the module name. This is useful when you want to access a wider range of dictionary functions.

Example:

import dict
set hasKey to dict.containskey(person, "name") // Checks if the key "name" exists in the dictionary.

Explanation:

  • We import the dict module and use it to call the containskey function. This method gives you access to all dictionary-related functions available in the module.

3. Using Function Import (Preferred for Specific Operations)

This method imports specific functions, allowing you to use only the functions you need. It makes your code more modular and reduces unnecessary imports.

Example:

import containskey from dict
set hasKey to containskey(person, "name") // Checks if the key "name" exists in the dictionary.

Explanation:

  • We import the containskey function directly from the dict module. This is efficient when you need to use specific functions and want to avoid importing unnecessary code.

Accessing Dictionary Keys, Values, and Items

  • Keys: You can access all keys in a dictionary using the keys() function.
  • Values: Use the values() function to get all values in the dictionary.
  • Items: Use the items() function to get both keys and values.

Example:

import keys, values, items from dict

set keysList to keys(person) // Returns a list of keys: ["name", "age", "city"]
set valuesList to values(person) // Returns a list of values: ["Ali", 25, "Lagos"]
set itemsList to items(person) // Returns a list of key-value pairs: [("name", "Ali"), ("age", 25), ("city", "Lagos")]

Modifying Dictionary Items

You can add, update, or delete key-value pairs in a dictionary.

Example - Adding or Updating Items:

set person["occupation"] to "Software Developer"  // Adds a new key-value pair
set person.age to 26 // Updates the value associated with the "age" key

Example - Removing Items:

import remove from dict
remove(person, "city") // Removes the key "city" from the dictionary

Explanation:

  • person["occupation"] adds a new key-value pair.
  • remove(person, "city") removes the "city" key from the dictionary.

Checking if a Key or Value Exists in a Dictionary

  • Checking Keys: Use the containskey function to check if a dictionary contains a specific key.
  • Checking Values: Use the containsvalue function to check if a dictionary contains a specific value.

Example:

set hasKey to containskey(person, "age")  // Returns true
set hasValue to containsvalue(person, 25) // Returns true

Summary of Dictionary Functions

Function NameExample SyntaxDescription
containskeycontainskey(person, "name")Checks if the dictionary contains the specified key.
containsvaluecontainsvalue(person, "Ali")Checks if the dictionary contains the specified value.
keyskeys(person)Returns a list of all the keys in the dictionary.
valuesvalues(person)Returns a list of all the values in the dictionary.
itemsitems(person)Returns a list of key-value pairs in the dictionary.
removeremove(person, "age")Removes a key-value pair from the dictionary.
lengthlength(person)Returns the number of key-value pairs in the dictionary.

Conclusion

Dictionaries are a powerful and flexible data structure that allows you to store and retrieve data efficiently using keys. By understanding how to access dictionary functions, modify items, and perform common operations like checking for keys and values, you can work with data in a more organized and efficient way.

With EasyBite's built-in dictionary functions, you can easily implement logic that involves mapping and associating data with meaningful identifiers, making your programs more dynamic and scalable.