Useful Python Code Snippets

1. Equal list: Checks if all elements in a list are equal. Use [1:] and [:-1] to compare all the values in the given list.

2. Unique items in list: Returns True if all the values in a list are unique, False otherwise. Use set() on the given list to remove duplicates, use len() to compare its length with the length of the list.

3. Mean: Returns the average of two or more numbers. Use sum() to sum all of the args provided, divide by len(args).

Mean after mapping: Returns the average of a list, after mapping each element to a value using the provided function. Use map() to map each element to the value returned by fn. Use sum() to sum all of the mapped values, divide by len(lst).

4. Split list: 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.

Split list by function: 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.

5. String byte length: Returns the length of a string in bytes. Use s.encode('utf-8') to encode the given string and return its length.

6. Camelcase string: Converts a string to camelcase. Use re.sub() to replace any - or _ with a space, using the regexp r"(_|-)+". Use title() to capitalize the first letter of each word convert the rest to lowercase. Finally, use replace() to remove spaces between words.

7. Capitalize string: Capitalizes the first letter of a string. Capitalize the first letter of the string and then add it with rest of the string. Omit the lower_rest parameter to keep the rest of the string intact, or set it to True to convert to lowercase.

Use s.title() to capitalize first letter of every word in the string.

Decapitalize: Decapitalize the first letter of the string and then add it with rest of the string. Omit the upper_rest parameter to keep the rest of the string intact, or set it to True to convert to uppercase.

8. Property of object: Given a predicate function, fn, and a prop string, this curried function will then take an object to inspect by calling the property and passing it to the predicate. Return a lambda function that takes an object and applies the predicate function, fn to the specified property.

9. Batch: 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 the created list.

10. Invert dictionary: Inverts a dictionary with non-unique hashable values. Use dictionary.items() in combination with a loop to map the values of the dictionary to keys using dictionary.setdefault(), list() and append() to create a list for each one.

11. Drop false value: Removes false values from a list. Use filter() to filter out false values (False, None, 0, and "").

12. Deep flatten list: Use recursion. Use isinstance() with collections.abc.Iterable to check if an element is iterable. If it is, apply deep_flatten() recursively, otherwise return [lst].

The Invisible Man | Machine Learning Engineer, Programmer, Tech Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store