Python tips and tricks

Python tips and tricks

According to Stack Overflow, Python is the fastest growing programming language. Netflix uses Python, IBM uses Python, and hundreds of other companies all use Python. Let’s not forget Dropbox. Dropbox is also created in Python.

Some of the advantages Python offers when compared to other programming languages are:

  • Compatible with major platforms and operating systems
  • Many open-source frameworks and tools
  • Readable and maintainable code
  • Robust standard library
  • Standard test-driven development

Lets look at some cool tips and tricks in python

Concatenating Strings

When you need to concatenate a list of strings, you can do this using a for loop by adding each element one by one. However, this would be very inefficient, especially if the list is long. In Python, strings are immutable, and thus the left and right strings would have to be copied into the new string for every pair of concatenation.

A better approach is to use the join() function as shown below:

Using ZIP When Working with Lists

Suppose you were given a task to combine several lists with the same length and print out the result? Again, here is a more generic way to get the desired result by utilizing zip()as shown in the code below:

Using itertools

The Python itertools module is a collection of tools for handling iterators. itertools has multiple tools for generating iterable sequences of input data. Here I will be using itertools.combinations() as an example. itertools.combinations() is used for building combinations. These are also the possible groupings of the input values.

Let’s take a real world example to make the above point clear.

Suppose there are four teams playing in a tournament. In the league stages every team plays against every other team. Your task is to generate all the possible teams that would compete against each other.

Let’s take a look at the code below:

Convert Two Lists Into a Dictionary

Let’s say we have two lists, one list contains names of the students and second contains marks scored by them. Let’s see how we can convert those two lists into a single dictionary. Using the zip function, this can be done using the code below:

Return Multiple Values From a Function

Python has the ability to return multiple values from a function call, something missing from many other popular programming languages. In this case the return values should be a comma-separated list of values and Python then constructs a tuple and returns this to the caller. As an example see the code below:

Using sorted() Function

Sorting any sequence is very easy in Python using the built-in method sorted()which does all the hard work for you. sorted()sorts any sequence (list, tuple) and always returns a list with the elements in sorted manner. Let’s take an example to sort a list of numbers in ascending order.

Taking another example, let’s sort a list of strings in descending order.

Map in golang

Map in golang

Map is a data structure which contains key value pair. In Golang make function returns a map of the given type, initialized and ready for use. In this article, we will be creating a map using make function and will perform operation on map like add, delete and iterate.


Stay tuned for more updates and tutorials !!!