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 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 26 if min_chunk_size > CHUNK_SIZE: 27 raise ValueError( 28 f"min_chunk_size {min_chunk_size} is greater than CHUNK_SIZE {CHUNK_SIZE}" 29 ) 30 31 for i in range(0, len(list_a), CHUNK_SIZE): 32 list_end = i + CHUNK_SIZE 33 if len(list_a) - list_end < min_chunk_size: 34 list_end = len(list_a) 35 yield list_a[i:list_end] 36 break 37 38 yield list_a[i:list_end] 39 40 41def excluding(list1: list, list2: list) -> list: 42 """ 43 Function to exclude elements from list1 that are present in list2. 44 45 Args: 46 list1 (list): The main list. 47 list2 (list): The list of elements to exclude. 48 49 Returns: 50 list: List1 with elements excluded. 51 """ 52 return [x for x in list1 if x not in list2] 53 54 55def intersecting(list1: list, list2: list) -> list: 56 """ 57 Function to find the intersection of two lists. 58 59 Args: 60 list1 (list): The first list. 61 list2 (list): The second list. 62 63 Returns: 64 list: Intersection of list1 and list2. 65 """ 66 return [value for value in list1 if value in list2] 67 68 69def first_or_none(my_list: list) -> typing.Any | None: 70 """Function to return the first element of a list or None if the list is empty.""" 71 if len(my_list) > 0: 72 return my_list[0] 73 return None 74 75 76def flatten(my_list: list) -> list: 77 """Function to flatten a list of lists into a single list.""" 78 return [item for sublist in my_list for item in sublist] 79 80 81def list_with_first_or_empty(my_list: list) -> list: 82 """Function to return a list containing the first element of the input list, or an empty list if the input list is empty.""" 83 first_or_none_value = first_or_none(my_list) 84 if first_or_none_value is None: 85 return [] 86 return [first_or_none_value] 87 88 89def not_none(my_list: list) -> list: 90 """Function to return a list with non-None elements.""" 91 return list(filter(lambda f: (f is not None), my_list)) 92 93 94def not_none_and_unique(my_list: list) -> list: 95 """Function to return a list with unique non-None elements.""" 96 return list(set(filter(lambda f: (f is not None), my_list))) 97 98 99def remove_empty_strings(list_of_strings: list[str]) -> list[str]: 100 """Function to remove empty strings from a list of strings.""" 101 parts_filtered = [] 102 for part in list_of_strings: 103 if part and len(part) > 0: 104 parts_filtered.append(part) 105 return parts_filtered 106 107 108def strip_strings(list_of_strings: list[str]) -> list[str]: 109 """Function to strip leading and trailing whitespace from strings in a list.""" 110 parts_stripped = [] 111 for part in list_of_strings: 112 parts_stripped.append(part.strip()) 113 return parts_stripped 114 115 116def unique(my_list: list) -> list: 117 """Function to return a list with unique elements from the input list.""" 118 return list(set(my_list))
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]
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.
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]
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.
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]
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.
70def first_or_none(my_list: list) -> typing.Any | 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
Function to return the first element of a list or None if the list is empty.
77def flatten(my_list: list) -> list: 78 """Function to flatten a list of lists into a single list.""" 79 return [item for sublist in my_list for item in sublist]
Function to flatten a list of lists into a single list.
82def list_with_first_or_empty(my_list: list) -> list: 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]
Function to return a list containing the first element of the input list, or an empty list if the input list is empty.
90def not_none(my_list: list) -> list: 91 """Function to return a list with non-None elements.""" 92 return list(filter(lambda f: (f is not None), my_list))
Function to return a list with non-None elements.
95def not_none_and_unique(my_list: list) -> list: 96 """Function to return a list with unique non-None elements.""" 97 return list(set(filter(lambda f: (f is not None), my_list)))
Function to return a list with unique non-None elements.
100def remove_empty_strings(list_of_strings: list[str]) -> list[str]: 101 """Function to remove empty strings from a list of strings.""" 102 parts_filtered = [] 103 for part in list_of_strings: 104 if part and len(part) > 0: 105 parts_filtered.append(part) 106 return parts_filtered
Function to remove empty strings from a list of strings.
109def strip_strings(list_of_strings: list[str]) -> list[str]: 110 """Function to strip leading and trailing whitespace from strings in a list.""" 111 parts_stripped = [] 112 for part in list_of_strings: 113 parts_stripped.append(part.strip()) 114 return parts_stripped
Function to strip leading and trailing whitespace from strings in a list.
117def unique(my_list: list) -> list: 118 """Function to return a list with unique elements from the input list.""" 119 return list(set(my_list))
Function to return a list with unique elements from the input list.