ExtList#

class ext_list.ExtList(iterable: list[T] = [])#

Note

The following class is used to describe each method of ExtList:

>>> class Person:
...     def __init__(self, name, age):
...         self.__name = name
...         self.__age = age
...
...     def introduce(self):
...         return f'{self.name} is {self.age} years old.'
...
...     def get_age_n_years_ago(self, n: int) -> int:
...        return self.age - n
...
...     @property
...     def name(self):
...         return self.__name
...
...     @property
...     def age(self):
...         return self.__age
...
...     def __repr__(self):
...         return f'Person('{self.name}', {self.age})'
append(element: T) None#

Append object to the end of the list.

extend(other: ExtList[T]) None#

Extend list by appending elements from the iterable.

insert(index: SupportsIndex, element: T) None#

Insert object before index.

extract(key: Callable[[T, Any], Any] | property | str | Hashable, *args: Any) ExtList[Any]#

Extracts and returns a list of values associated with the given key from the objects.

Parameters:
  • key (Callable[[T, Any], Any] | property | str | Hashable) – The key to extract values for. If the key is function, the callable will be executed and its result will be returned.

  • *args (Any) – If key is a function, the arguments will be passed to the function.

Returns:

A list of values associated with the given key.

Return type:

ExtList[Any]

Examples

The following example demonstrates how to use the ‘extract’ method.

>>> ext_list_1 = ExtList([{'a': 1}, {'a': 2}, {'a': 3}])
>>> ext_list_1.extract('a')
[1, 2, 3]
>>> ext_list_2 = ExtList([[1, 2], [3, 4], [5, 6]])
>>> ext_list_2.extract(0)
[1, 3, 5]
>>> ext_list_3 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35)])
>>> ext_list_3.extract(Person.name)
['Alice', 'Bob', 'Charlie']
>>> ext_list_3.extract(Person.introduce)
['Alice is 25 years old.', 'Bob is 30 years old.', 'Charlie is 35 years old.']
>>> ext_list_3.extract(Person.get_age_n_years_ago, 5)
[20, 25, 30]

Overrides _ListOperation.extract().

extract_duplicates(other: ExtList[T]) ExtList[T]#

Returns a list of objects that are in both the current object and the given object.

Parameters:

compare_ext_list (ExtList[T]) – The object to compare the current object to.

Returns:

A list of objects that are in both the current object and the given object. If no objects are found or the object is empty, an empty ExtList is returned.

Return type:

ExtList[T]

Examples

The following example demonstrates how to use the extract_duplicates method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}])
>>> ext_list_2 = ExtList([{'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.extract_duplicates(ext_list_2)
[{'name': 'Bob', 'age': 30}]

Overrides _ListOperation.extract_duplicates().

is_duplicate() bool#

Returns True if there are any duplicates in the current object, False otherwise.

Returns:

True if there are any duplicates in the current object, False otherwise.

Return type:

bool

Examples

The following example demonstrates how to use the is_duplicate method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Alice', 'age': 25}])
>>> ext_list_1.is_duplicate()
True
>>> ext_list_2 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_2.is_duplicate()
False

Overrides _ListOperation.is_duplicate().

one() T | None#

Returns the first object in the current object. If the object is empty, None is returned.

Returns:

The first object in the current object, or None if the object is empty.

Return type:

T or None

Examples

The following example demonstrates how to use the one method to return the first object in an ExtList:

>>> ext_list_1 = ExtList([1, 2, 3])
>>> ext_list_1.one()
1

The following example demonstrates how to use the one method to return None when the object is empty:

>>> ext_list_2 = ExtList([])
>>> ext_list_2.one()
None

Overrides _ListOperation.one().

first() T#

Returns the first object in the current object.

Returns:

The first object in the current object.

Return type:

T

Raises:

IndexError – If the object is empty.

Examples

The following example demonstrates how to use the first method to return the first object in an ExtList:

>>> ext_list_1 = ExtList([1, 2, 3])
>>> ext_list_1.first()
1

Overrides _ListOperation.first().

map(function: Callable[[T, Any], Any] | type, *args: Any) ExtList[Any]#

Apply a function or constructor to each element.

Parameters:
  • function (Callable[[T, Any], Any] | type) – The function or type to apply to each element.

  • *args (Any) – Additional arguments to pass to the function or type.

Returns:

A new ExtList containing the mapped values.

Return type:

ExtList[Any]

Examples

The following example demonstrates how to use the map method.

>>> ext_list_1 = ExtList([1, 2, 3])
>>> ext_list_1.map(float)
[1.0, 2.0, 3.0]

Overrides _ListOperation.map().

equal(key: Callable[[T, Any], Any] | property | str | Hashable, compare_target: Any, *args: Any) ExtList[T]#

Returns a list of objects that have the given key set to the given value.

Parameters:
  • key (Callable[[T, Any], Any] | property | str | Hashable) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_target (Any) – The value to compare the objects’ values to.

  • *args (Any) – If key is a function, the arguments will be passed to the function.

Returns:

A list of objects that have the given key set to the given value. If no objects are found or the object is empty, an empty ExtList is returned.

Return type:

ExtList[T]

Examples

The following example demonstrates how to use the equals method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.equals('age', 25)
[{'name': 'Alice', 'age': 25}]
>>> ext_list_2 = ExtList([{'name': 'Alice', 'graduated': None}, {'name': 'Bob', 'graduated': False}, {'name': 'Charlie', 'graduated': True}])
>>> ext_list_2.equals('graduated', None)
[{'name': 'Alice', 'graduated': None}]
>>> ext_list_3 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35), Person('David', 30)])
>>> ext_list_3.equals(Person.age, 30)
[, Person('Bob', 30), Person('David', 30)]
>>> ext_list_3.equals(Person.introduce, 'Alice is 25 years old.')
[Person('Alice', 25)]
>>> ext_list_3.equals(Person.get_age_n_years_ago, 20, 5)
[Person('Alice', 25)]

Overrides _OperatorOperation.equal().

not_equal(key: Callable[[T, Any], Any] | property | Hashable, compare_target: Any, *args: Any) ExtList[T]#

Returns a list of objects that do not have the given key set to the given value.

Parameters:
  • key (Callable[[T, Any], Any] | property | str | Hashable) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_target (Any) – The value to compare the objects’ values to.

  • *args (Any) – If key is a function, the arguments will be passed to the function.

Returns:

A list of objects that do not have the given key set to the given value. If no objects are found or the object is empty, an empty ExtList is returned.

Return type:

ExtList[T]

Examples

The following example demonstrates how to use the not_equals method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.not_equals('age', 25)
[{'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
>>> ext_list_2 = ExtList([{'name': 'Alice', 'graduated': None}, {'name': 'Bob', 'graduated': False}, {'name': 'Charlie', 'graduated': True}])
>>> ext_list_2.not_equals('graduated', None)
[{'name': 'Bob', 'graduated': False}, {'name': 'Charlie', 'graduated': True}]
>>> ext_list_3 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35), Person('David', 30)])
>>> ext_list_3.not_equals(Person.age, 30)
[Person('Alice', 25), Person('Charlie', 35)]
>>> ext_list_3.not_equals(Person.introduce, 'Alice is 25 years old.')
[Person('Bob', 30), Person('Charlie', 35), Person('David', 30)]
>>> ext_list_3.not_equals(Person.get_age_n_years_ago, 20, 5)
[Person('Bob', 30), Person('Charlie', 35), Person('David', 30)]

Overrides _OperatorOperation.not_equal().

greater(key: Callable[[T, Any], Any] | property | Hashable, compare_target: Any, *args: Any) ExtList[T]#

Return a list of objects that are greater than the specified compare_target, when the object is passed through the provided key function, property or hashable key.

Parameters:
  • key (Union[Callable[[T, Any], Any], property, Hashable]) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_target (Any) – The value to compare against.

  • *args (Any) – If key is a function, the arguments will be passed to the function.

Returns:

A list of objects that are greater than the compare_target, when the object is passed through the provided key.

Return type:

List[T]

Examples

The following example demonstrates how to use the greater method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.greater('age', 30)
[{'name': 'Charlie', 'age': 35}]
>>> ext_list_2 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35), Person('David', 30)])
>>> ext_list_2.greater(Person.age, 30)
[Person('Charlie', 35)]
>>> ext_list_2.greater(Person.get_age_n_years_ago, 25, 5)
[Person('Charlie', 35)]

Overrides _OperatorOperation.greater().

greater_or_equal(key: Callable[[T, Any], Any] | property | Hashable, compare_target: Any, *args: Any) ExtList[T]#

Return a list of objects that are greater than or equal the specified compare_target, when the object is passed through the provided key function, property or hashable key.

Parameters:
  • key (Union[Callable[[T, Any], Any], property, Hashable]) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_target (Any) – The value to compare against.

  • *args (Any) – Additional arguments to be passed to the key function.

Returns:

A list of objects that are greater than or equal the compare_target, when the object is passed through the provided key.

Return type:

List[T]

Examples

The following example demonstrates how to use the greater_or_equal method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.greater_or_equal('age', 30)
[{'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
>>> ext_list_2 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35), Person('David', 30)])
>>> ext_list_2.greater_or_equal(Person.age, 30)
[Person('Bob', 30), Person('Charlie', 35), Person('David', 30)]
>>> ext_list_2.greater_or_equal(Person.get_age_n_years_ago, 25, 5)
[Person('Bob', 30), Person('Charlie', 35), Person('David', 30)]

Overrides _OperatorOperation.greater_or_equal().

less(key: Callable[[T, Any], Any] | property | Hashable, compare_target: Any, *args: Any) ExtList[T]#

Return a list of objects that are less than the specified compare_target, when the object is passed through the provided key function, property or hashable key.

Parameters:
  • key (Union[Callable[[T, Any], Any], property, Hashable]) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_target (Any) – The value to compare against.

  • *args (Any) – Additional arguments to be passed to the key function.

Returns:

A list of objects that are less than the compare_target, when the object is passed through the provided key.

Return type:

List[T]

Examples

The following example demonstrates how to use the less method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.less('age', 30)
[{'name': 'Alice', 'age': 25}]
>>> ext_list_2 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35), Person('David', 30)])
>>> ext_list_2.less(Person.age, 30)
[Person('Alice', 25)]
>>> ext_list_2.less(Person.get_age_n_years_ago, 25, 5)
[Person('Alice', 25)]

Overrides _OperatorOperation.less().

less_or_equal(key: Callable[[T, Any], Any] | property | Hashable, compare_target: Any, *args: Any) ExtList[T]#

Return a list of objects that are less than or equal the specified compare_target, when the object is passed through the provided key function, property or hashable key.

Parameters:
  • key (Union[Callable[[T, Any], Any], property, Hashable]) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_target (Any) – The value to compare against.

  • *args (Any) – Additional arguments to be passed to the key function.

Returns:

A list of objects that are less than or equal the compare_target, when the object is passed through the provided key.

Return type:

List[T]

Examples

The following example demonstrates how to use the less_or_equal method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.less_or_equal('age', 30)
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
>>> ext_list_2 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35), Person('David', 30)])
>>> ext_list_2.less_or_equal(Person.age, 30)
[Person('Alice', 25), Person('Bob', 30), Person('David', 30)]
>>> ext_list_2.less_or_equal(Person.get_age_n_years_ago, 25, 5)
[Person('Alice', 25), Person('Bob', 30), Person('David', 30)]

Overrides _OperatorOperation.less_or_equal().

in_(key: Callable[[T, Any], Any] | property | str | Hashable, compare_target: list[Any], *args: Any) ExtList[T]#

Returns a list of objects that have the given key set to one of the given values.

Parameters:
  • key (Callable[[T, Any], Any] | property | str | Hashable) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_targets (list) – A list of values to compare the objects’ values to.

  • Any (*args) – If key is a function, the arguments will be passed to the function.

Returns:

A list of objects that have the given key set to one of the given values. If no objects are found or the object is empty, an empty ExtList is returned.

Return type:

ExtList[T]

Examples

The following example demonstrates how to use the in_ method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.in_('age', [25, 30])
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
>>> ext_list_2 = ExtList([{'name': 'Alice', 'graduated': None}, {'name': 'Bob', 'graduated': False}, {'name': 'Charlie', 'graduated': True}])
>>> ext_list_2.in_('graduated', [False, True])
[{'name': 'Bob', 'graduated': False}, {'name': 'Charlie', 'graduated': True}]
>>> ext_list_3 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35)])
>>> ext_list_3.in_(Person.age, [25, 35])
[Person(Alice, 25), Person(Charlie, 35)]
>>> ext_list_3.in_(Person.introduce, ['Alice is 25 years old.', 'Charlie is 35 years old.'])
[Person('Alice', 25), Person('Charlie', 35)]
>>> ext_list_3.in_(Person.get_age_n_years_ago, [20, 30], 5)
[Person('Alice', 25), Person('Charlie', 35)]

Overrides _OperatorOperation.in_().

not_in_(key: Callable[[T, Any], Any] | property | str | Hashable, compare_target: list[Any], *args: Any) ExtList[T]#

Returns a list of objects that do not have the given key set to any of the given values.

Parameters:
  • key (Callable[[T, Any], Any] | property | str | Hashable) – The key to compare values for. If the key is function, the callable will be executed and its result will be returned.

  • compare_targets (list) – A list of values to compare the objects’ values to.

  • *args (Any) – If key is a function, the arguments will be passed to the function.

Returns:

A list of objects that do not have the given key set to any of the given values. If no objects are found or the object is empty, an empty ExtList is returned.

Return type:

ExtList[T]

Examples

The following example demonstrates how to use the not_in_ method:

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])
>>> ext_list_1.not_in_('age', [25, 30])
[{'name': 'Charlie', 'age': 35}]
>>> ext_list_2 = ExtList([{'name': 'Alice', 'graduated': None}, {'name': 'Bob', 'graduated': False}, {'name': 'Charlie', 'graduated': True}])
>>> ext_list_2.not_in_('graduated', [False, True])
[{'name': 'Alice', 'graduated': None}]
>>> ext_list_3 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35)])
>>> ext_list_3.not_in_(Person.age, [25, 35])
[Person(Bob, 30)]
>>> ext_list_3.not_in_(Person.introduce, ['Alice is 25 years old.', 'Charlie is 35 years old.'])
[Person('Bob', 30)]
>>> ext_list_3.not_in_(Person.get_age_n_years_ago, [20, 30], 5)
[Person('Bob', 30)]

Overrides _OperatorOperation.not_in_().

to_dict(key: Callable[[T, Any], Any] | property | str | Hashable, *args: Any) dict[Hashable, T]#

Converts the current object to a dictionary, using the given key as the dictionary key.

Parameters:
  • key (Callable[[T, Any], Any] | property | str | Hashable) – The key to use as the dictionary key. If the key is function, the callable will be executed and its result will be returned.

  • Any (*args) – If key is a function, the arguments will be passed to the function.

Returns:

A dictionary of objects, using the given key as the dictionary key.

Return type:

dict[Hashable, T]

Examples

The following example demonstrates how to use the to_dict method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}])
>>> ext_list_1.to_dict('name')
{'Alice': {'name': 'Alice', 'age': 25}, 'Bob': {'name': 'Bob', 'age': 30}}
>>> ext_list_2 = ExtList([['Alice', 25], ['Bob', 30]])
>>> ext_list_2.to_dict(0)
{'Alice': ['Alice', 25], 'Bob': ['Bob', 30]}
>>> ext_list_3 = ExtList([Person('Alice', 25), Person('Bob', 30)])
>>> ext_list_3.to_dict(Person.name)
{'Alice': Person('Alice', 25), 'Bob': Person('Bob', 30)}
>>> ext_list_3.to_dict(Person.get_age_n_years_ago, 5)
{20: Person('Alice', 25), 25: Person('Bob', 30)}

Overrides _DictOperation.to_dict().

to_dict_list(keys: list[Callable[[T, Any], Any] | property | str | Hashable], arg_tuples: list[tuple[Any, ...]] = []) ExtList[dict[str | Hashable, Any]]#

Converts the objects into a list of dictionaries, where each dictionary contains the specified keys and their corresponding values from the object.

Parameters:
  • keys (list[Callable[[T, Any], Any] | property | str | Hashable]) – A list of keys to include in the dictionaries. Each key can be a function, property, string, or hashable object.

  • arg_tuples (list[tuple[Any, ...]], optional) – A list of argument tuples. Each tuple contains the arguments to be passed to the corresponding key function or property. Defaults to an empty list.

Returns:

A list of dictionaries, where each dictionary represents an element and contains the specified keys and their corresponding values.

Return type:

ExtList[dict[str | Hashable, Any]]

Examples

The following example demonstrates how to use the to_dict_list method.

>>> ext_list_1 = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}])
>>> ext_list_1.to_dict_list(['name'])
[{'name': 'Alice'}, {'name': 'Bob'}]
>>> ext_list_2 = ExtList([['Alice', 25], ['Bob', 30]])
>>> ext_list_2.to_dict_list([0])
[{0: 'Alice'}, {0: 'Bob'}]
>>> ext_list_3 = ExtList([Person('Alice', 25), Person('Bob', 30)])
>>> ext_list_3.to_dict_list([Person.name, Person.get_age_n_years_ago], [(), (5,)])
[{'name': 'Alice', 'get_age_n_years_ago': 20}, {'name': 'Bob', 'get_age_n_years_ago': 25}]

Overrides _DictOperation.to_dict_list().

to_dict_with_complex_keys(keys: list[Callable[[T, Any], Any] | property | str] | list[Hashable], arg_tuples: list[tuple[Any, ...]] = []) dict[tuple[Any, ...], T]#

Returns a dictionary of the objects in the ExtList with complex keys.

Parameters:
  • keys (List[Callable[[T, Any], Any] | property | str] | List[Hashable]) – A list of the keys for the dictionary.

  • arg_tuples (Tuple[Tuple[Any,...],...]) – A list of tuples of the arguments. If key is a function, the arguments will be passed to the function.

Returns:

A dictionary of the objects in the ExtList with complex keys.

Return type:

Dict[Tuple[Any, …], T]

Examples

The following example demonstrates how to use the to_dict_with_complex_keys method.

>>> ext_list_1 = ExtList([Person('Alice', 25), Person('Bob', 30), Person('Charlie', 35), Person('David', 30)])
>>> ext_list_1.to_dict_with_complex_keys([Person.name, Person.age])
{('Alice', 25): Person('Alice', 25),
 ('Bob', 30): Person('Bob', 30),
 ('Charlie', 35): Person('Charlie', 35),
 ('David', 30): Person('David', 30)}
>>> ext_list_1.to_dict_with_complex_keys(['name', Person.introduce, Person.get_age_n_years_ago], [(), (), (5,)])
{('Alice', 'Alice is 25 years old.', 20): Person('Alice', 25),
 ('Bob', 'Bob is 30 years old.', 25): Person('Bob', 30),
 ('Charlie', 'Charlie is 35 years old.', 30): Person('Charlie', 35),
 ('David', 'David is 30 years old.', 25): Person('David', 30)}

Overrides _DictOperation.to_dict_with_complex_keys().

dicts_to_instances(type_: TI) ExtList[TI]#

Convert a list of dictionaries to a list of instances of the given class.

Parameters:

type (Type[TI]) – The type of the instances to create.

Returns:

A new ExtList containing the instances.

Return type:

ExtList[TI]

Examples

The following example demonstrates how to use the dicts_to_instances method.

>>> ext_list_1 = ExtList([{'name': 'alice', 'age': 25}, {'name': 'bob', 'age': 30}, {'name': 'charlie', 'age': 35}])
>>> ext_list_1.dicts_to_instances(Person)
[Person('alice', 25), Person('bob', 30), Person('charlie', 35)]

Overrides _DictOperation.dicts_to_instances().

group_by_key(key: Callable[[T, Any], Any] | property | str | Hashable, *args: Any) dict[Hashable, ExtList[T]]#

Groups the objects of the list by a specified key.

Parameters:
  • key (Callable[[T, Any], Any] | property | str | Hashable) – The key to group the objects by. This can be a function, property, string, or hashable object.

  • *args (Any) – Additional arguments to pass to the key function or property.

Returns:

A dictionary of lists, where the keys are the result of applying the key function or property to the objects of the list, and the values are lists of objects with the same key.

Return type:

dict[Hashable, ExtList[T]]

Examples

The following example demonstrates how to use the dicts_to_instances method.

>>> ext_list_1 = ExtList([{'name': 'alice', 'age': 25}, {'name': 'bob', 'age': 30}, {'name': 'charlie', 'age': 35}, {'name': 'david', 'age': 30}])
>>> ext_list_1.group_by_key('age')
{25: [{'name': 'alice', 'age': 25}], 30: [{'name': 'bob', 'age': 30}, {'name': 'david', 'age': 30}], 35: [{'name': 'charlie', 'age': 35}]}

Overrides _DictOperation.group_by_key().

rename_keys(rename_keys: dict[Hashable, Hashable]) ExtList[T]#

Renames the keys in the objects based on the provided mapping dictionary.

Parameters:

rename_keys (dict[Hashable, Hashable]) – A dictionary that maps the keys to be renamed. The keys in the dictionary represent the original keys, while the corresponding values represent the new keys.

Returns:

A list of objects with the renamed keys.

Return type:

ExtList[T]

Raises:

TypeError – If the object is not indexable.

Examples

>>> ext_list = ExtList([{'name': 'alice', 'age': 25}, {'name': 'bob', 'age': 30}])
>>> ext_list.rename_keys({'name': 'Name', 'age': 'Age'})
[{'Name': 'alice', 'Age': 25}, {'Name': 'bob', 'Age': 30}]

Overrides _DictOperation.rename_keys().

map_for_keys(keys: list[Hashable], function: Callable[[Any], Any] | type, *args: Any) ExtList[dict[Any, Any]]#

Applies a function to specific keys of each element in the dictionary.

Parameters:
  • keys (list[Hashable]) – A list of hashable keys to apply the function to.

  • function (Callable[[Any], Any] | type) – The function or type to apply to the keys. It should accept the value of each key as the first argument, followed by optional args.

  • *args (Any) – Optional arguments to be passed to the function along with each key’s value.

Returns:

An instance of ExtList containing the modified dictionaries.

Raises:

TypeError – If the dictionary is not indexable.

Example

The following example demonstrates how to use the map_for_keys method.

>>> ext_list = ExtList([{'a': 1, 'b': 2, 'c': 3}])
>>> keys = ['a', 'b']
>>> function = lambda x, y: x + y
>>> args = (10,)
>>> ext_list.map_for_keys(keys, function, *args)
{'a': 11, 'b': 12, 'c': 3}

Overrides _DictOperation.map_for_keys().