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.
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, TypeVar 8 9T = TypeVar("T") 10 11 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 27 if min_chunk_size > CHUNK_SIZE: 28 raise ValueError( 29 f"min_chunk_size {min_chunk_size} is greater than CHUNK_SIZE {CHUNK_SIZE}" 30 ) 31 32 for i in range(0, len(list_a), CHUNK_SIZE): 33 list_end = i + CHUNK_SIZE 34 if len(list_a) - list_end < min_chunk_size: 35 list_end = len(list_a) 36 yield list_a[i:list_end] 37 break 38 39 yield list_a[i:list_end] 40 41 42def excluding(list1: list, list2: list) -> list: 43 """ 44 Function to exclude elements from list1 that are present in list2. 45 46 Args: 47 list1 (list): The main list. 48 list2 (list): The list of elements to exclude. 49 50 Returns: 51 list: List1 with elements excluded. 52 """ 53 return [x for x in list1 if x not in list2] 54 55 56def intersecting(list1: list, list2: list) -> list: 57 """ 58 Function to find the intersection of two lists. 59 60 Args: 61 list1 (list): The first list. 62 list2 (list): The second list. 63 64 Returns: 65 list: Intersection of list1 and list2. 66 """ 67 return [value for value in list1 if value in list2] 68 69 70def first_or_none(my_list: list[T]) -> T | None: 71 """Function to return the first element of a list or None if the list is empty.""" 72 if len(my_list) > 0: 73 return my_list[0] 74 return None 75 76 77def flatten(my_list: list[list[T]]) -> list[T]: 78 """Function to flatten a list of lists into a single list.""" 79 return [item for sublist in my_list for item in sublist] 80 81 82def list_with_first_or_empty(my_list: list[T]) -> list[T]: 83 """Function to return a list containing the first element of the input list, or an empty list if the input list is empty.""" 84 first_or_none_value = first_or_none(my_list) 85 if first_or_none_value is None: 86 return [] 87 return [first_or_none_value] 88 89 90def not_none(my_list: list[T]) -> list[T]: 91 """Function to return a list with non-None elements.""" 92 result: list[T] = [] 93 for item in my_list: 94 if item is not None: 95 result.append(item) 96 return result 97 98 99def not_none_and_unique(my_list: list[T]) -> list[T]: 100 """Function to return a list with unique non-None elements, preserving the order.""" 101 seen: list[T] = [] 102 result: list[T] = [] 103 for item in my_list: 104 if item is not None and item not in seen: 105 seen.append(item) 106 result.append(item) 107 return result 108 109 110def remove_empty_strings(list_of_strings: list[str]) -> list[str]: 111 """Function to remove empty strings from a list of strings.""" 112 parts_filtered = [] 113 for part in list_of_strings: 114 if part and len(part) > 0: 115 parts_filtered.append(part) 116 return parts_filtered 117 118 119def strip_strings(list_of_strings: list[str]) -> list[str]: 120 """Function to strip leading and trailing whitespace from strings in a list.""" 121 parts_stripped = [] 122 for part in list_of_strings: 123 parts_stripped.append(part.strip()) 124 return parts_stripped 125 126 127def unique(my_list: list[T]) -> list[T]: 128 """Return a list with unique elements from the input list, preserving order.""" 129 seen: list[T] = [] 130 result: list[T] = [] 131 for item in my_list: 132 if item not in seen: 133 seen.append(item) 134 result.append(item) 135 return result
13def chunk( 14 list_a: list, CHUNK_SIZE: int, min_chunk_size: int = 1 15) -> Generator[list, None, None]: 16 """ 17 Function to chunk a list into sublists of size n. 18 19 Args: 20 list_a (list): The input list to be chunked. 21 n (int): The size of each chunk. 22 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. 23 24 Yields: 25 list: Sublists of size n. 26 """ 27 28 if min_chunk_size > CHUNK_SIZE: 29 raise ValueError( 30 f"min_chunk_size {min_chunk_size} is greater than CHUNK_SIZE {CHUNK_SIZE}" 31 ) 32 33 for i in range(0, len(list_a), CHUNK_SIZE): 34 list_end = i + CHUNK_SIZE 35 if len(list_a) - list_end < min_chunk_size: 36 list_end = len(list_a) 37 yield list_a[i:list_end] 38 break 39 40 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.
43def excluding(list1: list, list2: list) -> list: 44 """ 45 Function to exclude elements from list1 that are present in list2. 46 47 Args: 48 list1 (list): The main list. 49 list2 (list): The list of elements to exclude. 50 51 Returns: 52 list: List1 with elements excluded. 53 """ 54 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.
57def intersecting(list1: list, list2: list) -> list: 58 """ 59 Function to find the intersection of two lists. 60 61 Args: 62 list1 (list): The first list. 63 list2 (list): The second list. 64 65 Returns: 66 list: Intersection of list1 and list2. 67 """ 68 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.
71def first_or_none(my_list: list[T]) -> T | None: 72 """Function to return the first element of a list or None if the list is empty.""" 73 if len(my_list) > 0: 74 return my_list[0] 75 return None
Function to return the first element of a list or None if the list is empty.
78def flatten(my_list: list[list[T]]) -> list[T]: 79 """Function to flatten a list of lists into a single list.""" 80 return [item for sublist in my_list for item in sublist]
Function to flatten a list of lists into a single list.
83def list_with_first_or_empty(my_list: list[T]) -> list[T]: 84 """Function to return a list containing the first element of the input list, or an empty list if the input list is empty.""" 85 first_or_none_value = first_or_none(my_list) 86 if first_or_none_value is None: 87 return [] 88 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.
91def not_none(my_list: list[T]) -> list[T]: 92 """Function to return a list with non-None elements.""" 93 result: list[T] = [] 94 for item in my_list: 95 if item is not None: 96 result.append(item) 97 return result
Function to return a list with non-None elements.
100def not_none_and_unique(my_list: list[T]) -> list[T]: 101 """Function to return a list with unique non-None elements, preserving the order.""" 102 seen: list[T] = [] 103 result: list[T] = [] 104 for item in my_list: 105 if item is not None and item not in seen: 106 seen.append(item) 107 result.append(item) 108 return result
Function to return a list with unique non-None elements, preserving the order.
111def remove_empty_strings(list_of_strings: list[str]) -> list[str]: 112 """Function to remove empty strings from a list of strings.""" 113 parts_filtered = [] 114 for part in list_of_strings: 115 if part and len(part) > 0: 116 parts_filtered.append(part) 117 return parts_filtered
Function to remove empty strings from a list of strings.
120def strip_strings(list_of_strings: list[str]) -> list[str]: 121 """Function to strip leading and trailing whitespace from strings in a list.""" 122 parts_stripped = [] 123 for part in list_of_strings: 124 parts_stripped.append(part.strip()) 125 return parts_stripped
Function to strip leading and trailing whitespace from strings in a list.
128def unique(my_list: list[T]) -> list[T]: 129 """Return a list with unique elements from the input list, preserving order.""" 130 seen: list[T] = [] 131 result: list[T] = [] 132 for item in my_list: 133 if item not in seen: 134 seen.append(item) 135 result.append(item) 136 return result
Return a list with unique elements from the input list, preserving order.