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 7 8def chunk(list_a, CHUNK_SIZE): 9 """ 10 Function to chunk a list into sublists of size n. 11 12 Args: 13 list_a (list): The input list to be chunked. 14 n (int): The size of each chunk. 15 16 Yields: 17 list: Sublists of size n. 18 """ 19 for i in range(0, len(list_a), CHUNK_SIZE): 20 yield list_a[i : i + CHUNK_SIZE] 21 22 23def excluding(list1, list2): 24 """ 25 Function to exclude elements from list1 that are present in list2. 26 27 Args: 28 list1 (list): The main list. 29 list2 (list): The list of elements to exclude. 30 31 Returns: 32 list: List1 with elements excluded. 33 """ 34 return [x for x in list1 if x not in list2] 35 36 37def intersecting(list1, list2): 38 """ 39 Function to find the intersection of two lists. 40 41 Args: 42 list1 (list): The first list. 43 list2 (list): The second list. 44 45 Returns: 46 list: Intersection of list1 and list2. 47 """ 48 return [value for value in list1 if value in list2] 49 50 51def first_or_none(my_list): 52 """Function to return the first element of a list or None if the list is empty.""" 53 if len(my_list) > 0: 54 return my_list[0] 55 return None 56 57 58def flatten(my_list): 59 """Function to flatten a list of lists into a single list.""" 60 return [item for sublist in my_list for item in sublist] 61 62 63def list_with_first_or_empty(my_list): 64 """Function to return a list containing the first element of the input list, or an empty list if the input list is empty.""" 65 first_or_none_value = first_or_none(my_list) 66 if first_or_none_value is None: 67 return [] 68 return [first_or_none_value] 69 70 71def not_none_and_unique(my_list): 72 """Function to return a list with unique non-None elements.""" 73 return list(set(filter(lambda f: (f is not None), my_list))) 74 75 76def remove_empty_strings(list_of_strings): 77 """Function to remove empty strings from a list of strings.""" 78 parts_filtered = [] 79 for part in list_of_strings: 80 if part and len(part) > 0: 81 parts_filtered.append(part) 82 return parts_filtered 83 84 85def strip_strings(list_of_strings): 86 """Function to strip leading and trailing whitespace from strings in a list.""" 87 parts_stripped = [] 88 for part in list_of_strings: 89 parts_stripped.append(part.strip()) 90 return parts_stripped 91 92 93def unique(my_list): 94 """Function to return a list with unique elements from the input list.""" 95 return list(set(my_list))
9def chunk(list_a, CHUNK_SIZE): 10 """ 11 Function to chunk a list into sublists of size n. 12 13 Args: 14 list_a (list): The input list to be chunked. 15 n (int): The size of each chunk. 16 17 Yields: 18 list: Sublists of size n. 19 """ 20 for i in range(0, len(list_a), CHUNK_SIZE): 21 yield list_a[i : i + CHUNK_SIZE]
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.
Yields: list: Sublists of size n.
24def excluding(list1, list2): 25 """ 26 Function to exclude elements from list1 that are present in list2. 27 28 Args: 29 list1 (list): The main list. 30 list2 (list): The list of elements to exclude. 31 32 Returns: 33 list: List1 with elements excluded. 34 """ 35 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.
38def intersecting(list1, list2): 39 """ 40 Function to find the intersection of two lists. 41 42 Args: 43 list1 (list): The first list. 44 list2 (list): The second list. 45 46 Returns: 47 list: Intersection of list1 and list2. 48 """ 49 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.
52def first_or_none(my_list): 53 """Function to return the first element of a list or None if the list is empty.""" 54 if len(my_list) > 0: 55 return my_list[0] 56 return None
Function to return the first element of a list or None if the list is empty.
59def flatten(my_list): 60 """Function to flatten a list of lists into a single list.""" 61 return [item for sublist in my_list for item in sublist]
Function to flatten a list of lists into a single list.
64def list_with_first_or_empty(my_list): 65 """Function to return a list containing the first element of the input list, or an empty list if the input list is empty.""" 66 first_or_none_value = first_or_none(my_list) 67 if first_or_none_value is None: 68 return [] 69 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.
72def not_none_and_unique(my_list): 73 """Function to return a list with unique non-None elements.""" 74 return list(set(filter(lambda f: (f is not None), my_list)))
Function to return a list with unique non-None elements.
77def remove_empty_strings(list_of_strings): 78 """Function to remove empty strings from a list of strings.""" 79 parts_filtered = [] 80 for part in list_of_strings: 81 if part and len(part) > 0: 82 parts_filtered.append(part) 83 return parts_filtered
Function to remove empty strings from a list of strings.
86def strip_strings(list_of_strings): 87 """Function to strip leading and trailing whitespace from strings in a list.""" 88 parts_stripped = [] 89 for part in list_of_strings: 90 parts_stripped.append(part.strip()) 91 return parts_stripped
Function to strip leading and trailing whitespace from strings in a list.
94def unique(my_list): 95 """Function to return a list with unique elements from the input list.""" 96 return list(set(my_list))
Function to return a list with unique elements from the input list.