Data Science and Data Proicessing

PROGRAMMING

🔝Top 29 Useful Python Snippets 🔝That Save You Time

Here are my favorite 29 Python snippets that actually save me time as a developer

Top 29 Useful Python Snippets That Save You Time
Source: Pixabay

Python is one of the most popular languages ​​used by many in data science and machine learning, web development, scripting, automation, and more. One of the reasons for this popularity is its simplicity and ease of learning.

If you are reading this, you are most likely already using Python, or at least interested in it.

In this article, we’ll take a quick look at 29 short code snippets that you can understand and master incredibly quickly. Go!

👉 1. Checking for uniqueness

The next method checks if there are duplicate items in the given list. It uses a property set()that removes duplicate items from the list:

👉 2. Anagram

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 another word or phrase, usually using all the original letters exactly once:

👉 3. Memory

And this can be used to check the memory usage of an object:

👉 4. Size in bytes

The method returns the length of the string in bytes:

👉 5. Print the string N times

This snippet can be used to output a string nonce without the need to use loops for this:

👉 6. Makes the first letters of words large

And here is the register. The snippet uses a method title()to capitalize each word in a string:

👉 7. Separation

This method splits the list into smaller lists of the specified size:

👉 8. Removing false values

So you remove the false values ( False, None, 0and «») from the list using filter():

👉 9. Counting

The following code can be used to transpose a 2D array:

👉 10. Chain comparison

You can do multiple comparisons with all kinds of operators in one line:

👉 11. Separate with comma

The following snippet can be used to convert a list of strings to a single string, where each item from the list is separated by commas:

👉 12. Count the vowels

This method counts the number of vowels (“a”, “e”, “i”, “o”, “u”) found in the string:

👉 13. Converting the first letter of a string to lowercase

Use to convert the first letter of your specified string to lowercase:

👉 14. Anti-aliasing

The following methods flatten out a potentially deep list using recursion:

👉 15. Difference

The method finds the difference between the two iterations, keeping only the values ​​that are in the first:

👉 16. The difference between lists

The following method returns the difference between the two lists after applying this function to each element of both lists:

👉 17. Chained function call

You can call multiple functions on one line:

👉 18. Finding Duplicates

This code checks to see if there are duplicate values ​​in the list using the fact that set()it only contains unique values:

👉 19. Combine two dictionaries

The following method can be used to combine two dictionaries:

👉 20. Convert two lists to a dictionary

Now let’s get down to converting two lists into a dictionary:

👉 21. Using `enumerate`

The snippet shows what you can use enumerate()to get both values ​​and indices of lists:

👉 22. Time spent

Use to calculate the time it takes for a specific code to run:

👉 23. Try / else

You can use elseas part of a block try:

👉 24. The element that appears most often

This method returns the most frequent item that appears in the list:

👉 25. Palindrome

The method checks if the given string is a palindrome:

👉 26. Calculator without if-else

The following snippet shows how to write a simple calculator without the need for conditions if-else:

👉 27. Shuffle

This code can be used to randomize the order of items in a list. Note that shuffleworks in place and returns None:

👉 28. Change values

A really quick way to swap two variables without the need for an extra one:

👉 29. Get default value for missing keys

The code shows how you can get the default value if the key you are looking for is not included in the dictionary:

Read More

If you found this article helpful, click the💚 or 👏 button below or share the article on Facebook so your friends can benefit from it too.

More from my stories:

+ Recent posts