Skip to content
DataLakehouse.help
GitHub

Python Tips and Tricks

Part 1

PurposeSyntaxExample
Swap two variables’ valuesa, b = b, aa, b = 5, 10 -> a, b = 10, 5
Find the maximum or minimum of a listmax_value = max(lst)max_value = max([3, 7, 2, 8]) -> 8
Check if all elements in a list are trueall_true = all(lst)all_true = all([True, True, False]) -> False
Create a list with a range of valuesmy_list = list(range(1, 6))my_list = [1, 2, 3, 4, 5]
Find the index of the maximum elementindex = lst.index(max(lst))index = [3, 7, 2, 8].index(8) -> 3
Create a one-liner for-loop[x*2 for x in range(1, 6)][2, 4, 6, 8, 10]
Remove duplicates from a listunique_lst = list(set(lst))unique_lst = [1, 2, 2, 3, 3, 4] -> [1, 2, 3, 4]
Read lines from a file into a listwith open('file.txt') as f: lines = f.readlines()lines contains lines from ‘file.txt’
Create a dictionary with default valuesmy_dict = defaultdict(int)my_dict['key'] returns 0 if ‘key’ doesn’t exist
Find the factorial of a numberimport math; factorial = math.factorial(n)factorial = math.factorial(5) -> 120
Iterate over dictionary key-value pairsfor key, value in my_dict.items():for k, v in {'a': 1, 'b': 2}.items():
Use a ternary conditional expressionvalue = 'Yes' if condition else 'No'result = 'Pass' if score >= 70 else 'Fail'
Create a copy of a list without referencesnew_lst = old_lst.copy()new_lst is a separate list from old_lst
Find the length of a list or stringlength = len(my_list)length = len('Hello') -> 5
Reverse a string or listreversed_str = my_str[::-1]reversed_str = 'Hello'[::-1] -> 'olleH'
Convert a string to uppercase or lowercaseupper_str = my_str.upper()upper_str = 'hello'.upper() -> 'HELLO'
Check if a string starts or ends with a substringmy_str.startswith('prefix')my_str.endswith('suffix')
Merge two dictionariesdict1.update(dict2)dict1 now contains keys and values from dict2
Count occurrences of an element in a listcount = my_list.count(element)count = [1, 2, 2, 3, 3, 4].count(2) -> 2
Check if a value exists in a list or stringexists = value in my_listexists = 'e' in 'Hello' -> True
Use a generator expression(x**2 for x in range(1, 6))Returns a generator object for squared values
Get the current date and timefrom datetime import datetime; now = datetime.now()now contains current date and time
Use list comprehension to filter elements[x for x in my_list if x % 2 == 0]Filters even numbers from my_list
Find the most common element in a listfrom collections import Counter; most_common = Counter(my_list).most_common(1)Returns a list of the most common element(s)

Part 2

PurposeSyntaxExample
Check if a string is emptyis_empty = not bool(my_str)is_empty = not bool('Hello') -> False
Sort a list in placemy_list.sort()my_list.sort() sorts my_list in ascending order
Create a list of unique elementsunique_list = list(set(my_list))unique_list = list(set([1, 2, 2, 3])) -> [1, 2, 3]
Find the index of an element in a listindex = my_list.index(element)index = ['a', 'b', 'c'].index('b') -> 1
Iterate over a list in reverse orderfor item in reversed(my_list):Iterates over my_list in reverse
Find the absolute value of a numberabs_value = abs(number)abs_value = abs(-5) -> 5
Remove whitespace from the beginning and end of a stringtrimmed_str = my_str.strip()Removes leading and trailing whitespace
Use a dictionary to count element occurrencescounts = {item: my_list.count(item) for item in my_list}Creates a dictionary of element counts
Check if all elements in a list are uniqueis_unique = len(my_list) == len(set(my_list))is_unique = len([1, 2, 3, 2]) == len(set([1, 2, 3, 2])) -> False
Convert a list of strings to a single stringjoined_str = ''.join(my_list)joined_str = ''.join(['Hello', ' ', 'World']) -> 'Hello World'
Use a dictionary to map values to keysmy_dict = {value: key for key, value in key_value_pairs}Creates a dictionary mapping values to keys
Find the square root of a numberimport math; sqrt_value = math.sqrt(number)sqrt_value = math.sqrt(9) -> 3.0
Generate a list of numbers within a rangenumber_list = list(range(start, stop, step))number_list = list(range(1, 10, 2)) -> [1, 3, 5, 7, 9]
Find the least common multiple (LCM) of two numbersimport math; lcm = abs(a * b) // math.gcd(a, b)lcm = abs(12 * 18) // math.gcd(12, 18) -> 36
Combine string formatting and variablesformatted_str = f"My name is {name}, and I am {age} years old."Uses f-strings for dynamic string formatting
Use a dictionary to provide default valuesvalue = my_dict.get(key, default_value)value = my_dict.get('key', 'Not found')
Filter elements from a list with a lambda functionfiltered_list = list(filter(lambda x: x % 2 == 0, my_list))Filters even numbers from my_list
Create a function with default argumentspython def greet(name="Guest"): return f"Hello, {name}!"Defines a function with a default argument
Use a generator function to save memorypython def fibonacci(n): a, b = 0, 1; while n > 0: yield a; a, b, n = b, a + b, n - 1Generates Fibonacci numbers efficiently
Find the unique elements shared by two listscommon_elements = list(set(list1) & set(list2))Finds common elements between list1 and list2
Create a dictionary with keys from a list and default valuesmy_dict = dict.fromkeys(key_list, default_value)my_dict = dict.fromkeys(['a', 'b'], 0) -> {'a': 0, 'b': 0}
Remove all occurrences of an element from a listmy_list = [x for x in my_list if x != element]Removes all occurrences of element from my_list
Use a lambda function for quick calculationssquare = lambda x: x ** 2square(4) returns 16
Check if a string contains only digitsis_digit_str = my_str.isdigit()is_digit_str = '12345'.isdigit() -> True

Part 3

PurposeSyntaxExample
Find the index of the first occurrence of an elementindex = my_list.index(element)index = ['a', 'b', 'c'].index('b') -> 1
Use the enumerate function to get index and valuefor index, value in enumerate(my_list):Access index and value in a loop
Check if a list is emptyis_empty = not bool(my_list)is_empty = not bool([]) -> True
Use a set for efficient membership testsmy_set = set(my_list); exists = value in my_setexists = 3 in {1, 2, 3, 4} -> True
Swap case in a stringswapped_str = my_str.swapcase()swapped_str = 'Hello'.swapcase() -> 'hELLO'
Use the zip function to combine two listscombined_list = list(zip(list1, list2))combined_list contains pairs from list1 and list2
Convert a string to a list of characterschar_list = list(my_str)char_list = list('Python') -> ['P', 'y', 't', 'h', 'o', 'n']
Use the collections.Counter for counting elementsfrom collections import Counter; counts = Counter(my_list)Counts occurrences of elements
Find the number of lines in a text stringline_count = len(text.splitlines())line_count = len("Line 1\nLine 2\nLine 3".splitlines()) -> 3
Create a dictionary with multiple keys mapping to the same valuemy_dict = dict.fromkeys(keys, value)my_dict = dict.fromkeys(['a', 'b'], 0) -> {'a': 0, 'b': 0}
Use a dictionary comprehension to create dictionaries{key: value for key, value in zip(keys, values)}Creates a dictionary from two lists
Remove leading and trailing characters from a stringstripped_str = my_str.strip("characters")Removes specified characters from the beginning and end
Convert a list of dictionaries into a dictionary of listsresult_dict = {key: [d[key] for d in dict_list] for key in dict_list[0]}Reorganizes data into a dictionary of lists
Find the most frequent element in a listfrom statistics import mode; most_frequent = mode(my_list)Finds the most frequent element
Use list comprehension for conditional filtering[x for x in my_list if condition]Filters elements based on a condition
Join elements of a list into a string with a separatorjoined_str = separator.join(my_list)joined_str = ','.join(['apple', 'banana', 'cherry']) -> 'apple,banana,cherry'
Check if a string contains only letters and digitsis_alphanumeric = my_str.isalnum()is_alphanumeric = 'Python3'.isalnum() -> True
Calculate the sum of elements in a listtotal = sum(my_list)total = sum([1, 2, 3, 4, 5]) -> 15
Extract keys or values from a dictionarykeys = list(my_dict.keys())values = list(my_dict.values())
Replace multiple characters in a stringtranslated_str = my_str.translate(str.maketrans(old_chars, new_chars))Replaces multiple characters
Generate random numbersimport random; random_number = random.randint(min_value, max_value)random_number = random.randint(1, 100)
Check if a file existsimport os; exists = os.path.isfile(file_path)exists = os.path.isfile('my_file.txt') -> True
Calculate the length of the longest string in a listmax_length = max(len(item) for item in my_list)Finds the length of the longest string
Use a list comprehension to flatten a nested list[item for sublist in nested_list for item in sublist]Flattens a list of lists
Iterate over a string in reverse orderfor char in reversed(my_str):Iterates over characters in reverse order

Part 4

PurposeSyntaxExample
Convert a string to title casetitle_case_str = my_str.title()title_case_str = 'python is cool'.title() -> 'Python Is Cool'
Use the collections.defaultdict for default valuesfrom collections import defaultdict; my_dict = defaultdict(default_value)Provides default values for dictionary keys
Find the most common characters in a stringfrom collections import Counter; common_chars = Counter(my_str).most_common(3)Finds the 3 most common characters
Generate a list of prime numberspython def is_prime(n): return n > 1 and all(n % i != 0 for i in range(2, int(n**0.5) + 1)); primes = [x for x in range(2, limit) if is_prime(x)]Generates a list of prime numbers up to a limit
Calculate the average of elements in a listaverage = sum(my_list) / len(my_list)average = sum([1, 2, 3, 4, 5]) / 5 -> 3.0
Find the index of the last occurrence of an elementindex = len(my_list) - my_list[::-1].index(element) - 1index = ['a', 'b', 'c', 'b'].index('b') -> 3
Perform multiple operations in a single linepython result = (value * 2 for value in my_list if value % 2 == 0)Applies multiple operations to filter and modify a list
Use timeit for measuring execution timepython import timeit; execution_time = timeit.timeit(stmt="your_code_here", number=1000)Measures the execution time of code
Replace a specific occurrence of a substring in a stringnew_str = my_str.replace(old_substring, new_substring, occurrence)Replaces only the specified occurrence
Use zip with * for transposing a matrixtransposed_matrix = list(zip(*matrix))Transposes a 2D matrix
Calculate the greatest common divisor (GCD)import math; gcd = math.gcd(a, b)gcd = math.gcd(24, 36) -> 12
Count occurrences of a substring in a stringcount = my_str.count(substring)count = 'hello world'.count('l') -> 3
Find the second largest element in a listsecond_largest = max(filter(lambda x: x != max(my_list), my_list))Finds the second largest element
Use functools.reduce() for cumulative operationsfrom functools import reduce; product = reduce(lambda x, y: x * y, my_list)Calculates the product of elements
Check if all elements in a list are identicalare_identical = all(x == my_list[0] for x in my_list)are_identical = all([1, 1, 1, 1]) -> True
Flatten a list of lists using itertools.chainfrom itertools import chain; flat_list = list(chain(*list_of_lists))Flattens a list of lists
Find the index of the minimum element in a listindex = my_list.index(min(my_list))index = [5, 2, 8, 1, 7].index(1) -> 3
Create a list of numbers using a generator expression[x for x in range(start, stop)]Generates a list of numbers within a range
Count trailing zeros in a factorial numberpython def count_trailing_zeros(n): count = 0; while n % 10 == 0: n //= 10; count += 1; return countcount_trailing_zeros(12000) returns 3
Use a deque for efficient queue operationsfrom collections import deque; queue = deque()Implements a queue with fast operations
Check if a string contains only whitespace charactersis_whitespace = my_str.isspace()is_whitespace = ' \t\n'.isspace() -> True
Split a list into chunks of a specific sizepython chunked_list = [my_list[i:i + chunk_size] for i in range(0, len(my_list), chunk_size)]Splits my_list into chunks of size chunk_size
Use the random.sample() function for random samplingimport random; sample = random.sample(population, k)Generates a random sample of size k from population
Create a function that accepts an arbitrary number of argumentsdef my_function(*args):Defines a function that accepts a variable number of arguments
Use the zip function for parallel iterationpython for item1, item2 in zip(list1, list2):Iterates over two lists in parallel
Retrieve the last n elements from a listlast_n_elements = my_list[-n:]last_n_elements = [1, 2, 3, 4, 5][-3:] -> [3, 4, 5]