Experienced programmers too find it not enough when it comes to discovering new tricks
Python is a programming language known for its simplicity and easy-to-read syntax with a variety of frameworks and a strong ecosystem, that python developers heavily depend on. Many times, programmers come across a piece of code on forums like stack overflow or GitHub making them wonder, how that code works. Indeed, Python is a versatile language with an infinite number of possibilities. Even experienced programmers find it not enough when it comes to discovering new tricks in Python programming. Here we list 10 Python coding tips to know in 2022.
Flatten the lists :
Converting a 2D list into a 1D list, called flattening the list usually requires nested loops, list comprehensions, recursion, built-in functions, or importing libraries in Python depending on the regularity and depth of the nested lists, the easiest of all being using imported libraries. Here is how it can be done.
- import itertools
- a = [[1, 2], [3, 4], [5, 6]]
- b = list(itertools.chain.from_iterable(a))
- [1, 2, 3, 4, 5, 6]
Reverse a list
In this technique, a copy of the list is made and the list is not sorted in place. Creating a copy requires more space to hold all of the existing elements. This exhausts more memory. Here we are using the slicing technique to reverse our list in Python.
Combining different lists
It is easy to aggregate the contents of the container class using a normal zip function. There are times, though, when numerous lists and contained lists are required as index components, and you must merge them. Though this is an odd circumstance, the solution is straightforward.
for x, y in zip(a, b):
Negative indexing lists
Giving a negative number as a parameter inside a list function we can remove the last elements of that list and we get a new list.
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Launch web server
To launch a web server on any port, a simple command goes a long way in simplifying the code. All that you need to do is, set the port from range 0 to 65353.
# Run Web Server
python -m http.server 7000
Easy value swapping
Swapping of values of two variables is usually done using a temporary variable. There is a trick where you do not need a temp variable to achieve swapping.
#using temp variable
a = 5
b = 6
temp = a
a = b
b = temp
a, b = b, a
Do you need to open a website from your default browser? Then, the next tip will assist you with this.
# Opening a Website
Detecting New Elements
A nested loop is a common method to detect new elements in lists. However, with a set() data structure it is possible to detect the unique elements in any two lists.
# Find New Elements
list1 = [4, 5, 6, 8, 11, 13]
list2 = [4, 5, 6]
new = list(set(list1) – set(list2))
print(new) # [8, 11, 13]
Checking and analyzing the memory unit of an object
In Python, everything is an object, from variables to lists and dictionaries everything is treated as an object. Here is one easy way to get its value.
Transposing a matrix
In Python, transposing is usually implemented using a nested list (list inside a list) treating each element as a row of the matrix. However, with the zip function, it can be achieved in a few lines.
mat = [[8, 9, 10], [11, 12, 13]]
for row in new_mat: