cornsnake.util_list

Functions for manipulating lists of data. Functions include chunking lists, excluding elements from one list that are present in another, finding the intersection of two lists, and various other list operations.

Documentation

  1"""
  2Functions for manipulating lists of data. Functions include chunking lists, excluding elements from one list that are present in another, finding the intersection of two lists, and various other list operations.
  3
  4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_list.html)
  5"""
  6
  7from typing import Generator
  8import typing
  9
 10
 11def chunk(
 12    list_a: list, CHUNK_SIZE: int, min_chunk_size: int = 1
 13) -> Generator[list, None, None]:
 14    """
 15    Function to chunk a list into sublists of size n.
 16
 17    Args:
 18    list_a (list): The input list to be chunked.
 19    n (int): The size of each chunk.
 20    min_chunk_size: The minimum size for a chunk. Set to avoid a smaller chunk at the end, instead add its contents to the penultimate chunk.
 21
 22    Yields:
 23    list: Sublists of size n.
 24    """
 25    for i in range(0, len(list_a), CHUNK_SIZE):
 26        list_end = i + CHUNK_SIZE
 27        if len(list_a) - list_end < min_chunk_size:
 28            list_end = len(list_a)
 29            yield list_a[i:list_end]
 30            break
 31
 32        yield list_a[i:list_end]
 33
 34
 35def excluding(list1: list, list2: list) -> list:
 36    """
 37    Function to exclude elements from list1 that are present in list2.
 38
 39    Args:
 40    list1 (list): The main list.
 41    list2 (list): The list of elements to exclude.
 42
 43    Returns:
 44    list: List1 with elements excluded.
 45    """
 46    return [x for x in list1 if x not in list2]
 47
 48
 49def intersecting(list1: list, list2: list) -> list:
 50    """
 51    Function to find the intersection of two lists.
 52
 53    Args:
 54    list1 (list): The first list.
 55    list2 (list): The second list.
 56
 57    Returns:
 58    list: Intersection of list1 and list2.
 59    """
 60    return [value for value in list1 if value in list2]
 61
 62
 63def first_or_none(my_list: list) -> typing.Any | None:
 64    """Function to return the first element of a list or None if the list is empty."""
 65    if len(my_list) > 0:
 66        return my_list[0]
 67    return None
 68
 69
 70def flatten(my_list: list) -> list:
 71    """Function to flatten a list of lists into a single list."""
 72    return [item for sublist in my_list for item in sublist]
 73
 74
 75def list_with_first_or_empty(my_list: list) -> list:
 76    """Function to return a list containing the first element of the input list, or an empty list if the input list is empty."""
 77    first_or_none_value = first_or_none(my_list)
 78    if first_or_none_value is None:
 79        return []
 80    return [first_or_none_value]
 81
 82
 83def not_none(my_list: list) -> list:
 84    """Function to return a list with non-None elements."""
 85    return list(filter(lambda f: (f is not None), my_list))
 86
 87
 88def not_none_and_unique(my_list: list) -> list:
 89    """Function to return a list with unique non-None elements."""
 90    return list(set(filter(lambda f: (f is not None), my_list)))
 91
 92
 93def remove_empty_strings(list_of_strings: list[str]) -> list[str]:
 94    """Function to remove empty strings from a list of strings."""
 95    parts_filtered = []
 96    for part in list_of_strings:
 97        if part and len(part) > 0:
 98            parts_filtered.append(part)
 99    return parts_filtered
100
101
102def strip_strings(list_of_strings: list[str]) -> list[str]:
103    """Function to strip leading and trailing whitespace from strings in a list."""
104    parts_stripped = []
105    for part in list_of_strings:
106        parts_stripped.append(part.strip())
107    return parts_stripped
108
109
110def unique(my_list: list) -> list:
111    """Function to return a list with unique elements from the input list."""
112    return list(set(my_list))
def chunk( list_a: list, CHUNK_SIZE: int, min_chunk_size: int = 1) -> Generator[list, NoneType, NoneType]:
12def chunk(
13    list_a: list, CHUNK_SIZE: int, min_chunk_size: int = 1
14) -> Generator[list, None, None]:
15    """
16    Function to chunk a list into sublists of size n.
17
18    Args:
19    list_a (list): The input list to be chunked.
20    n (int): The size of each chunk.
21    min_chunk_size: The minimum size for a chunk. Set to avoid a smaller chunk at the end, instead add its contents to the penultimate chunk.
22
23    Yields:
24    list: Sublists of size n.
25    """
26    for i in range(0, len(list_a), CHUNK_SIZE):
27        list_end = i + CHUNK_SIZE
28        if len(list_a) - list_end < min_chunk_size:
29            list_end = len(list_a)
30            yield list_a[i:list_end]
31            break
32
33        yield list_a[i:list_end]

Function to chunk a list into sublists of size n.

Args: list_a (list): The input list to be chunked. n (int): The size of each chunk. min_chunk_size: The minimum size for a chunk. Set to avoid a smaller chunk at the end, instead add its contents to the penultimate chunk.

Yields: list: Sublists of size n.

def excluding(list1: list, list2: list) -> list:
36def excluding(list1: list, list2: list) -> list:
37    """
38    Function to exclude elements from list1 that are present in list2.
39
40    Args:
41    list1 (list): The main list.
42    list2 (list): The list of elements to exclude.
43
44    Returns:
45    list: List1 with elements excluded.
46    """
47    return [x for x in list1 if x not in list2]

Function to exclude elements from list1 that are present in list2.

Args: list1 (list): The main list. list2 (list): The list of elements to exclude.

Returns: list: List1 with elements excluded.

def intersecting(list1: list, list2: list) -> list:
50def intersecting(list1: list, list2: list) -> list:
51    """
52    Function to find the intersection of two lists.
53
54    Args:
55    list1 (list): The first list.
56    list2 (list): The second list.
57
58    Returns:
59    list: Intersection of list1 and list2.
60    """
61    return [value for value in list1 if value in list2]

Function to find the intersection of two lists.

Args: list1 (list): The first list. list2 (list): The second list.

Returns: list: Intersection of list1 and list2.

def first_or_none(my_list: list) -> typing.Any | None:
64def first_or_none(my_list: list) -> typing.Any | None:
65    """Function to return the first element of a list or None if the list is empty."""
66    if len(my_list) > 0:
67        return my_list[0]
68    return None

Function to return the first element of a list or None if the list is empty.

def flatten(my_list: list) -> list:
71def flatten(my_list: list) -> list:
72    """Function to flatten a list of lists into a single list."""
73    return [item for sublist in my_list for item in sublist]

Function to flatten a list of lists into a single list.

def list_with_first_or_empty(my_list: list) -> list:
76def list_with_first_or_empty(my_list: list) -> list:
77    """Function to return a list containing the first element of the input list, or an empty list if the input list is empty."""
78    first_or_none_value = first_or_none(my_list)
79    if first_or_none_value is None:
80        return []
81    return [first_or_none_value]

Function to return a list containing the first element of the input list, or an empty list if the input list is empty.

def not_none(my_list: list) -> list:
84def not_none(my_list: list) -> list:
85    """Function to return a list with non-None elements."""
86    return list(filter(lambda f: (f is not None), my_list))

Function to return a list with non-None elements.

def not_none_and_unique(my_list: list) -> list:
89def not_none_and_unique(my_list: list) -> list:
90    """Function to return a list with unique non-None elements."""
91    return list(set(filter(lambda f: (f is not None), my_list)))

Function to return a list with unique non-None elements.

def remove_empty_strings(list_of_strings: list[str]) -> list[str]:
 94def remove_empty_strings(list_of_strings: list[str]) -> list[str]:
 95    """Function to remove empty strings from a list of strings."""
 96    parts_filtered = []
 97    for part in list_of_strings:
 98        if part and len(part) > 0:
 99            parts_filtered.append(part)
100    return parts_filtered

Function to remove empty strings from a list of strings.

def strip_strings(list_of_strings: list[str]) -> list[str]:
103def strip_strings(list_of_strings: list[str]) -> list[str]:
104    """Function to strip leading and trailing whitespace from strings in a list."""
105    parts_stripped = []
106    for part in list_of_strings:
107        parts_stripped.append(part.strip())
108    return parts_stripped

Function to strip leading and trailing whitespace from strings in a list.

def unique(my_list: list) -> list:
111def unique(my_list: list) -> list:
112    """Function to return a list with unique elements from the input list."""
113    return list(set(my_list))

Function to return a list with unique elements from the input list.