dictionary#
- swordfish.dictionary(data: Any = None, *, key_type: DataType = None, val_type: DataType = None, ordered: bool = True) Dictionary#
- swordfish.dictionary(*, keys: Any = None, vals: Any = None, key_type: DataType = None, val_type: DataType = None, ordered: bool = True) Dictionary
- swordfish.dictionary(*, key_type: DataType = None, val_type: DataType = None, ordered: bool = True) Dictionary
Creates a Swordfish Dictionary from a Python object, from separate keys and values, or as an empty dictionary with specified key and value types.
This function provides three modes of initialization:
If
datais provided, it converts a given Python object into a Dictionary, optionally with the specifiedkey_typeandval_type.If
keysandvalsare provided, it constructs a Dictionary from these separate sequences, optionally with the specifiedkey_typeandval_type.If neither
datanorkeysandvalsare provided, it initializes an empty Dictionary with the specifiedkey_typeandval_type.
- Parameters:
data (Any, optional) – The Python object to initialize the Dictionary. Defaults to None.
keys (Any, optional) – The keys for the Dictionary (used when
datais None). Defaults to None.vals (Any, optional) – The values for the Dictionary (used when
datais None). Defaults to None.key_type (DataType, optional) – The data type of the Dictionary keys. Defaults to None.
val_type (DataType, optional) – The data type of the Dictionary values. Defaults to None.
ordered (bool, optional) – Whether to maintain the insertion order of the Dictionary elements. Defaults to True.
- Returns:
A Swordfish Dictionary initialized based on the provided arguments.
- Return type:
Examples
- Creating a Dictionary from existing data:
>>> import swordfish as sf >>> my_dict = {"name": "Alice", "age": 25} >>> sf.dictionary(my_dict, key_type="STRING", val_type="ANY", ordered=True) Dictionary(name->Alice age->25 , key_type=STRING, val_type=ANY)
- Creating a Dictionary using separate keys and values:
>>> sf.dictionary(keys=[1, 2, 3], vals=['a', 'b', 'c'], ... key_type="INT", val_type="STRING", ordered=False) Dictionary(3->c 2->b 1->a , key_type=INT, val_type=STRING)
- Creating an empty Dictionary with specified key and value types:
>>> sf.dictionary(key_type="STRING", val_type="INT", ordered=True) Dictionary(, key_type=STRING, val_type=INT)