Photo by Jantine Doornbos on Unsplash

Python represents one of the most popular languages that many people use it in data science and machine learning, web development, scripting, automation, etc.

Part of the reason for this popularity is its simplicity and easiness to learn it.

If you are reading this, then it is highly likely that you already use Python or at least have an interest in it.

In this article, we will briefly see 30 short code snippets that you can understand and learn in 30 seconds or less.

1. All unique

The following method checks whether the given list has duplicate elements. It uses the property of set() which removes duplicate elements from the list.

2. Anagrams

This method can be used to check if two strings are anagrams. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

3. Memory

This snippet can be used to check the memory usage of an object.

4. Byte size

This method returns the length of a string in bytes.

5. Print a string N times

This snippet can be used to print a string n times without having to use loops to do it.

6. Capitalize first letters

This snippet simply uses the method title() to capitalize first letters of every word in a string.

7. Chunk

This method chunks a list into smaller lists of a specified size.

8. Compact

This method removes falsy values (False, None, 0 and “”) from a list by using filter().

9. Count by

This snippet can be used to transpose a 2D array.

10. Chained comparison

You can do multiple comparisons with all kinds of operators in a single line.

11. Comma-separated

This snippet can be used to turn a list of strings into a single string with each element from the list separated by commas.

12. Get vowels

This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.

13. Decapitalize

This method can be used to turn the first letter of the given string into lowercase.

14. Flatten

The following methods flatten a potentially deep list using recursion.

15. Difference

This method finds the difference between two iterables by keeping only the values that are in the first one.

16. Difference by

The following method returns the difference between two lists after applying a given function to each element of both lists.

17. Chained function call

You can call multiple functions inside a single line.

18. Has duplicates

The following method checks whether a list has duplicate values by using the fact that set() contains only unique elements.

19. Merge two dictionaries

The following method can be used to merge two dictionaries.

In Python 3.5 and above, you can also do it like the following:

20. Convert two lists into a dictionary

The following method can be used to convert two lists into a dictionary.

21. Use enumerate

This snippet shows that you can use enumerate to get both the values and the indexes of lists.

22. Time spent

This snippet can be used to calculate the time it takes to execute a particular code.

23. Try else

You can have an else clause as part of a try/except block, which is executed if no exception is thrown.

24. Most frequent

This method returns the most frequent element that appears in a list.

25. Palindrome

This method checks whether a given string is a palindrome.

26. Calculator without if-else

The following snippet shows how you can write a simple calculator without the need to use if-else conditions.

27. Shuffle

This snippet can be used to randomize the order of the elements in a list. Note that shuffle works in place, and returns None.

28. Spread

This method flattens a list similarly like [].concat(…arr) in JavaScript.

29. Swap values

This is a really quick way for swapping two variables without having to use an additional one.

30. Get default value for missing keys

This snippet shows how you can get a default value in case a key you are looking for is not included in the dictionary.