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.

  1. import itertools
  2. a = [[1, 2], [3, 4], [5, 6]]
  3. b = list(itertools.chain.from_iterable(a))
  4. print(b)
  5. Output:
  6. [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.

a=[“10”,”9″,”8″,”7″]

print(a[::-1])

Output:

10

9

8

7

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.

a=[‘a’,’b’,’c’,’d’]

b=[‘e’,’f’,’g’,’h’]

for x, y in zip(a, b):

print(x,y)

Output:

a e

b f

c g

d h

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]

a[-3:-1]

Output:

[8, 9]

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

#new way

a, b = b, a

Opening Website

Do you need to open a website from your default browser? Then, the next tip will assist you with this.

# Opening a Website

import webbrowser

webbrowser.open(“https://rubikscode.net/“)

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.

import sys

a=10

print(sys.getsizeof(a))

Output:

28

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]]

new_mat=zip(*mat)

for row in new_mat:

print(row)

Output:

(8, 11)

(9, 12)

(10, 13)