20 Python Snippets You Should Learn in 2021

20 Python Snippets You Should Learn in 2021

Published at - Oct 27, 2021

Python is one of the most popular languages used by many in Data Science, machine learning, web development, scripting automation, etc. One of the reasons for this popularity is its simplicity and its ease of learning. If you are reading this article you are most likely already using Python or at least interested in it.

1. Check for Uniqueness in Python List

This method can be used to check if there are duplicate items in a given list.

Refer to the code below:

<iframe src="https://medium.com/media/a302c7dcdbf485d7c2e2e30f98e539a6" frameborder=0></iframe>

2. anagram()

An anagram in the English language is a word or phrase formed by rearranging the letters of another word or phrase.

The anagram() method can be used to check if two Strings are anagrams.

<iframe src="https://medium.com/media/6fc74732c212cbb5062638e46035b2cd" frameborder=0></iframe>

3. Memory

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

<iframe src="https://medium.com/media/ffb60a935eb70e100df9f4130d55e2e3" frameborder=0></iframe>

4. Size in Bytes

The method shown below returns the length of the String in bytes:

<iframe src="https://medium.com/media/e2b8e8d760e38281f8efa025d8026b5d" frameborder=0></iframe>

5. Print the String n Times

This snippet can be used to display a String n times without using loops:

<iframe src="https://medium.com/media/8e40926dfb12539e3835dce113be8c02" frameborder=0></iframe>

6. Convert the First Letters of Words to Uppercase

The snippet uses a method title() to capitalize each word in a String:

<iframe src="https://medium.com/media/a87d368eb366291727ab8b2451da1e65" frameborder=0></iframe>

7. Separation

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

<iframe src="https://medium.com/media/57522c059bcfbb222fbbbbef357a24b8" frameborder=0></iframe>

8. Removal of False Values

So you remove the false values (False, None, 0, and ‘’) from the list using filter() method:

<iframe src="https://medium.com/media/0c61c467522b1ee03eed6d9873b8a117" frameborder=0></iframe>

9. To Count

This is done as demonstrated below:

<iframe src="https://medium.com/media/b3f360e0e5eaae7da21278a41f7c49d1" frameborder=0></iframe>

10. Chain Comparison

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

<iframe src="https://medium.com/media/6481b7bb40d33b36f3de527b3aff96c9" frameborder=0></iframe>

11. Separate With Comma

Convert a list of Strings to a single String, where each item from the list is separated by commas:

<iframe src="https://medium.com/media/46a16912951e6538db72b63832a36155" frameborder=0></iframe>

12. Count the Vowels

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

<iframe src="https://medium.com/media/59c39f24291e2a8ecce2081049ebeac4" frameborder=0></iframe>

13. Convert the First Letter of a String to Lowercase

Use the lower() method to convert the first letter of your specified String to lowercase:

<iframe src="https://medium.com/media/14611f225e01dfed511046492f1ad814" frameborder=0></iframe>

14. Anti-aliasing

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

<iframe src="https://medium.com/media/a2fc982023be652c5325f51361bfbc8b" frameborder=0></iframe>

15. difference()

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

<iframe src="https://medium.com/media/65dd8d825dd20be773962aed996cbded" frameborder=0></iframe>

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:

<iframe src="https://medium.com/media/95bb31a9baff1a69b5f516d73425521d" frameborder=0></iframe>

17. Chained Function Call

You can call multiple functions in one line:

<iframe src="https://medium.com/media/05a9e62f4dfcb6472c3eabebb4d9e9cf" frameborder=0></iframe>

18. Find Duplicates

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

<iframe src="https://medium.com/media/a629273a4d2387f5ba18350e45ebe38c" frameborder=0></iframe>

19. Combine Two Dictionaries

The following method can be used to combine two dictionaries:

<iframe src="https://medium.com/media/e4c55c892071a335c75cd33ce8fb3422" frameborder=0></iframe>

20. Convert Two Lists to a Dictionary

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

<iframe src="https://medium.com/media/ff9bbbf67bac0ac5f6865a15c06f715f" frameborder=0></iframe>


In this article, I have covered the top 20 Python snippets which are very useful while developing any Python application. These snippets can help you save time and let you code faster. I hope you like this article. Please clap and follow me for more articles like this. Thank you for reading.

For Further Reading: 30 Useful Python List Snippets Important reusable Python list snippets while developing any applicationPython.plainenglish.io Automate WhatsApp Messages with Python Schedule and send WhatsApp messages at a particular time automaticallyPython.plainenglish.io How to Create a Screen and Webcam Recorder using Python Create your own screen and webcam recorder with just 30 lines of Python code.Python.plainenglish.io Another 30 Very Useful Python String Snippets 30 Python String snippets — Part 2Python.plainenglish.io

More content at plainenglish.io

Related Posts -

How to Retrieve Emails Using IMAP in Python
How to Retrieve Emails Usin ...

Good day there, In a recent piece, I discussed How to use Python SMTP to se...

Learn Data Types in Python
Learn Data Types in Python ...

Python is a welcome change from JavaScript because it has a huge standard l...

Create a Python-based Internet Speed Test GUI App: A Guide
Create a Python-based Inter ...

Whenever we face an internet issue, the first thing we do is an internet sp...

The Best Python Compiler to Run Code Online
The Best Python Compiler to ...

If you have Python Compiler installed on your system and want to run a prog...

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile