Original code copyright (C) 2009-2022 Rudolf Cardinal (

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Functions for dealing with lists (and generators/iterables).

cardinal_pythonlib.lists.chunks(x: List[Any], n: int) Iterable[List[Any]][source]

Yield successive n-sized chunks from the list x.

  • x – input list

  • n – chunk size


successive chunks of size n

cardinal_pythonlib.lists.contains_duplicates(values: Iterable[Any]) bool[source]

Does the iterable contain any duplicate values?

cardinal_pythonlib.lists.count_bool(blist: Iterable[Any]) int[source]

Counts the number of “truthy” members of the input list.


blist – list of booleans or other truthy/falsy things


number of truthy items

cardinal_pythonlib.lists.delete_elements_by_index(x: List[Any], indices: Sequence[int]) None[source]

Deletes (in place) objects from a list, by (zero-based) index values.

  • x – list to modify

  • indices – zero-based index values at which to delete elements of x

Note: - If you pass duplicate values, unexpected results will occur. - If you pass out-of-bound indices, IndexError will be raised.

After: - - -

cardinal_pythonlib.lists.filter_unique(seq: Iterable[Any]) Iterable[Any][source]

Filters the input sequence, yielding only unique values.

cardinal_pythonlib.lists.flatten_list(x: List[Any]) List[Any][source]

Converts a list of lists into a flat list.


x – list of lists


flat list

As per

cardinal_pythonlib.lists.index_list_for_sort_order(x: List[Any], key: Callable[[Any], Any] | None = None, reverse: bool = False) List[int][source]

Returns a list of indexes of x, IF x WERE TO BE SORTED.

  • x – data

  • key – function to be applied to the data to generate a sort key; this function is passed as the key= parameter to sorted(); the default is itemgetter(1)

  • reverse – reverse the sort order?


list of integer index values


z = ["a", "c", "b"]
index_list_for_sort_order(z)  # [0, 2, 1]
index_list_for_sort_order(z, reverse=True)  # [1, 2, 0]
q = [("a", 9), ("b", 8), ("c", 7)]
index_list_for_sort_order(q, key=itemgetter(1))
cardinal_pythonlib.lists.sort_list_by_index_list(x: List[Any], indexes: List[int]) None[source]

Re-orders x by the list of indexes of x, in place.


from cardinal_pythonlib.lists import sort_list_by_index_list

z = ["a", "b", "c", "d", "e"]
sort_list_by_index_list(z, [4, 0, 1, 2, 3])
z  # ["e", "a", "b", "c", "d"]
cardinal_pythonlib.lists.unique_list(seq: Iterable[Any]) List[Any][source]

Returns a list of all the unique elements in the input list.


seq – input list


list of unique elements

As per