30 seconds of python

List

Click on a snippet's name to view its code.

all_equal

Check if all elements in a list are equal.

Use [1:] and [:-1] to compare all the values in the given list.

all_unique

Returns True if all the values in a flat list are unique, False otherwise.

Use set() on the given list to remove duplicates, compare its length with the length of the list.

bifurcate

Splits values into two groups. If an element in filter is True, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.

Use list comprehension and enumerate() to add elements to groups, based on filter.

bifurcate_by

Splits values into two groups according to a function, which specifies which group an element in the input list belongs to. If the function returns True, the element belongs to the first group; otherwise, it belongs to the second group.

Use list comprehension to add elements to groups, based on fn.

chunk

Chunks a list into smaller lists of a specified size.

Use list() and range() to create a list of the desired size. Use map() on the list and fill it with splices of the given list. Finally, return use created list.

compact

Removes falsey values from a list.

Use filter() to filter out falsey values (False, None, 0, and "").

count_by

Groups the elements of a list based on the given function and returns the count of elements in each group.

Use map() to map the values of the given list using the given function. Iterate over the map and increase the element count each time it occurs.

count_occurences

Counts the occurrences of a value in a list.

Increment a counter for every item in the list that has the given value and is of the same type.

deep_flatten

Deep flattens a list.

Use recursion. Define a function, spread, that uses either list.extend() or list.append() on each element in a list to flatten it. Use list.extend() with an empty list and the spread function to flatten a list. Recursively flatten each element that is a list.

difference

Returns the difference between two iterables.

Create a set from b, then use list comprehension on a to only keep values not contained in the previously created set, _b.

difference_by

Returns the difference between two lists, after applying the provided function to each list element of both.

Create a set by applying fn to each element in b, then use list comprehension in combination with fn on a to only keep values not contained in the previously created set, _b.

every

Returns True if the provided function returns True for every element in the list, False otherwise.

Use all() in combination with map and fn to check if fn returns True for all elements in the list.

every_nth

Returns every nth element in a list.

Use [nth-1::nth] to create a new list that contains every nth element of the given list.

filter_non_unique

Filters out the non-unique values in a list.

Use list comprehension and list.count() to create a list containing only the unique values.

filter_unique

Filters out the unique values in a list.

Use list comprehension and list.count() to create a list containing only the non-unique values.

flatten

Flattens a list of lists once.

Use nested list comprehension to extract each value from sub-lists in order.

group_by

Groups the elements of a list based on the given function.

Use map() and fn to map the values of the list to the keys of an object. Use list comprehension to map each element to the appropriate key.

has_duplicates

Returns True if there are duplicate values in a flast list, False otherwise.

Use set() on the given list to remove duplicates, compare its length with the length of the list.

head

Returns the head of a list.

use lst[0] to return the first element of the passed list.

initial

Returns all the elements of a list except the last one.

Use lst[0:-1] to return all but the last element of the list.

initialize_2d_list

Initializes a 2D list of given width and height and value.

Use list comprehension and range() to generate h rows where each is a list with length h, initialized with val. If val is not provided, default to None.

initialize_list_with_range

Initializes a list containing the numbers in the specified range where start and end are inclusive with their common difference step.

Use list and range() to generate a list of the appropriate length, filled with the desired values in the given range. Omit start to use the default value of 0. Omit step to use the default value of 1.

initialize_list_with_values

Initializes and fills a list with the specified value.

Use list comprehension and range() to generate a list of length equal to n, filled with the desired values. Omit val to use the default value of 0.

intersection

Returns a list of elements that exist in both lists.

Create a set from a and b, then use the built-in set operator & to only keep values contained in both sets, then transform the set back into a list.

intersection_by

Returns a list of elements that exist in both lists, after applying the provided function to each list element of both.

Create a set by applying fn to each element in b, then use list comprehension in combination with fn on a to only keep values contained in both lists.

last

Returns the last element in a list.

use lst[-1] to return the last element of the passed list.

longest_item

Takes any number of iterable objects or objects with a length property and returns the longest one. If multiple objects have the same length, the first one will be returned.

Use max() with len as the key to return the item with the greatest length.

max_element_index

Returns the pindex of the element with the maximum value in a list.

Use max() and list.index() to get the maximum value in the list and return its index.

max_n

Returns the n maximum elements from the provided list. If n is greater than or equal to the provided list's length, then return the original list (sorted in descending order).

Use sorted() to sort the list, [:n] to get the specified number of elements. Omit the second argument, n, to get a one-element list.

min_n

Returns the n minimum elements from the provided list. If n is greater than or equal to the provided list's length, then return the original list (sorted in ascending order).

Use sorted() to sort the list,[:n]to get the specified number of elements. Omit the second argument,n`, to get a one-element list.

most_frequent

Returns the most frequent element in a list.

Use set(list) to get the unique values in the list combined with max() to find the element that has the most appearances.

none

Returns False if the provided function returns True for at least one element in the list, True otherwise.

Use all() and fn to check if fn returns False for all the elements in the list.

offset

Moves the specified amount of elements to the end of the list.

Use lst[offset:] and lst[:offset] to get the two slices of the list and combine them before returning.

sample

Returns a random element from an array.

Use randint() to generate a random number that corresponds to an index in the list, return the element at that index.

similarity

Returns a list of elements that exist in both lists.

Use list comprehension on a to only keep values contained in both lists.

some

Returns True if the provided function returns True for at least one element in the list, False otherwise.

Use any() in combination with map() and fn to check if fn returns True for any element in the list.

spread

Flattens a list, by spreading its elements into a new list.

Loop over elements, use list.extend() if the element is a list, list.append() otherwise.

symmetric_difference

Returns the symmetric difference between two iterables, without filtering out duplicate values.

Create a set from each list, then use list comprehension on each one to only keep values not contained in the previously created set of the other.

symmetric_difference_by

Returns the symmetric difference between two lists, after applying the provided function to each list element of both.

Create a set by applying fn to each element in every list, then use list comprehension in combination with fn on each one to only keep values not contained in the previously created set of the other.

tail

Returns all elements in a list except for the first one.

Return lst[1:] if the list's length is more than 1, otherwise, return the whole list.

transpose

Returns the transpose of a two-dimensional list.

Use *lst to get the passed list as tuples. Use zip() in combination with list() to create the transpose of the given two-dimensional list.

union

Returns every element that exists in any of the two lists once.

Create a set with all values of a and b and convert to a list.

union_by

Returns every element that exists in any of the two lists once, after applying the provided function to each element of both.

Create a set by applying fn to each element in a, then use list comprehension in combination with fn on b to only keep values not contained in the previously created set, _a. Finally, create a set from the previous result and a and transform it into a list

unique_elements

Returns the unique elements in a given list.

Create a set from the list to discard duplicated values, then return a list from it.

zip

Creates a list of elements, grouped based on the position in the original lists.

Use max combined with list comprehension to get the length of the longest list in the arguments. Loop for max_length times grouping elements. If lengths of lists vary, use fill_value (defaults to None).