cardinal_pythonlib.lists¶
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
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 listx
.Parameters: - x – input list
- n – chunk size
Yields: 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.
Parameters: blist – list of booleans or other truthy/falsy things Returns: 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.
Parameters: - 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: - https://thispointer.com/python-remove-elements-from-list-by-index/ - https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists - https://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index
-
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.
Parameters: x – list of lists Returns: flat list As per https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
-
cardinal_pythonlib.lists.
index_list_for_sort_order
(x: List[Any], key: Callable[[Any], Any] = None, reverse: bool = False) → List[int][source]¶ Returns a list of indexes of
x
, IFx
WERE TO BE SORTED.Parameters: - x – data
- key – function to be applied to the data to generate a sort key; this
function is passed as the
key=
parameter tosorted()
; the default isitemgetter(1)
- reverse – reverse the sort order?
Returns: list of integer index values
Example:
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 ofindexes
ofx
, in place.Example:
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"]