Cool Python Tricks

This post provides some cool python tricks.

(Stay tuned, as I may update the tricks while I plow in my deep learning garden)

With increase in popularity of python, more and more features are becoming available for python coding. Using this features makes writing the code in fewer lines and cleaner. In this article we will see 10 such python tricks which are very frequently used and most useful.

Reversing a List

We can simply reverse a given list by using a reverse() function. It handles both numeric and string data types present in the list.

Example

List = ["Shriya", "Lavina","Sampreeti" ]
List.reverse()
print(List)

Output

Running the above code gives us the following result −

['Sampreeti', 'Lavina', 'Shriya']

Print list elements in any order

If you need to print the values of a list in different orders, you can assign the list to a series of variables and programmatically decide the order in which you want to print the list.

Example

List = [1,2,3]
w, v, t = List
print(v, w, t )
print(t, v, w )

Output

Running the above code gives us the following result −

(2, 1, 3)
(3, 2, 1)

Using Generators Inside Functions

We can use generators directly inside a function to writer shorter and cleaner code. In the below example we find the sum using a generator directly as an argument to the sum function.

Example

sum(i for i in range(10) )

Output

Running the above code gives us the following result −

45

*Using the zip() function

When we need to join many iterator objects like lists to get a single list we can use the zip function. The result shows each item to be grouped with their respective items from the other lists.

Example

Year = (1999, 2003, 2011, 2017)
Month = ("Mar", "Jun", "Jan", "Dec")
Day = (11,21,13,5)
print zip(Year,Month,Day)

Output

Running the above code gives us the following result −

[(1999, 'Mar', 11), (2003, 'Jun', 21), (2011, 'Jan', 13), (2017, 'Dec', 5)]

Swap two numbers using a single line of code

Swapping of numbers usually requires storing of values in temporary variables. But with this python trick we can do that using one line of code and without using any temporary variables.

Example

x,y = 11, 34
print x
print y
x,y = y,x
print x
print y

Output

Running the above code gives us the following result −

11
34
34
11

Transpose a Matrix

Transposing a matrix involves converting columns into rows. In python we can achieve it by designing some loop structure to iterate through the elements in the matrix and change their places or we can use the following script involving zip function in conjunction with the * operator to unzip a list which becomes a transpose of the given matrix.

Example

x = [[31,17],
[40 ,51],
[13 ,12]]
print (zip(*x))

Output

Running the above code gives us the following result −

[(31, 40, 13), (17, 51, 12)]

*Print a string N Times

The usual approach in any programming language to print a string multiple times is to design a loop. But python has a simple trick involving a string and a number inside the print function.

Example

str ="Point";
print(str * 3);

Output

Running the above code gives us the following result −

PointPointPoint

*Reversing List Elements Using List Slicing

List slicing is a very powerful technique in python which can also be used to reverse the order of elements in a list.

Example

#Reversing Strings
list1 = ["a","b","c","d"]
print list1[::-1]

# Reversing Numbers
list2 = [1,3,6,4,2]
print list2[::-1]

Output

Running the above code gives us the following result −

['d', 'c', 'b', 'a']
[2, 4, 6, 3, 1]

Find the Factors of a Number

When we are need of the factors of a number, required for some calculation or analysis, we can design a small loop which will check the divisibility of that number with the iteration index.

Example

f = 32
print "The factors of",x,"are:"
for i in range(1, f + 1):
   if f % i == 0:
print(i)

Output

Running the above code gives us the following result −

The factors of 32 are:
1
2
4
8
16
32

*Checking the Usage of Memory

We can check the amount of memory consumed by each variable that we declare by using the getsizeof() function. As you can see below, different string lengths will consume different amount of memory.

Example

import sys
a, b, c,d = "abcde" ,"xy", 2, 15.06
print(sys.getsizeof(a))
print(sys.getsizeof(b))
print(sys.getsizeof(c))
print(sys.getsizeof(d))

Output

Running the above code gives us the following result −

38
35
24
24

 

 

References:

https://www.tutorialspoint.com/10-interesting-python-cool-tricks (Pradeep Elance Published on 08-Aug-2019 10:22:06)

https://www.tutorialspoint.com/questions/category/Python

Printing data in nice format in python

This post introduces how to print data in a nice formatted way in python using the built-in module pprint.

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a well-formatted and more readable way.

>>> from pprint import pprint

>>> my_list = ["1","2","3","4"]

>>> print(my_list)

['1', '2', '3', '4']

>>> pprint(my_list)

['1', '2', '3', '4']

# You may wonder the output of pprint is not working, because it looks the same as the output of print.

# however, the output is entirely correct and expected. From the pprint module documentation:
# The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width.

#You could set the width keyword argument to 1 to force every key-value pair being printed on a separate line: 

>>> pprint(my_list, width =1)

['1',

 '2',

 '3',

 '4']

>>> 

Note that if you use import pprint, instead of fromp pprint import pprint, use the following:

>>> import pprint 
>>> my_list = ["1","2","3","4"]  
>>> pprint.pprint(my_list)

see below for an example of printing json data.

>>> from pprint import pprint

>>> my_json = { "fruit": "Apple", "size": "Large", "color": "Red" }

>>> print(my_json)

{'color': 'Red', 'fruit': 'Apple', 'size': 'Large'}

>>> pprint(my_json)

{'color': 'Red', 'fruit': 'Apple', 'size': 'Large'}

>>> pprint(my_json, width =1)

{'color': 'Red',

 'fruit': 'Apple',

 'size': 'Large'}

>>> from pprint import pprint

>>> my_json = {'children': [], 'lastName': 'Smith', 'phoneNumbers': [{'number': '212 555-1234', 'type': 'home'}, {'number': '646 555-4567', 'type': 'office'}, {'number': '123 456-7890', 'type': 'mobile'}], 'address': {'city': 'New York', 'postalCode': '10021-3100', 'state': 'NY', 'streetAddress': '21 2nd Street'}, 'firstName': 'John', 'age': 27}

>>> print(my_json)

{'children': [], 'lastName': 'Smith', 'phoneNumbers': [{'number': '212 555-1234', 'type': 'home'}, {'number': '646 555-4567', 'type': 'office'}, {'number': '123 456-7890', 'type': 'mobile'}], 'address': {'city': 'New York', 'streetAddress': '21 2nd Street', 'state': 'NY', 'postalCode': '10021-3100'}, 'firstName': 'John', 'age': 27}

>>> pprint(my_json)

{'address': {'city': 'New York',

             'postalCode': '10021-3100',

             'state': 'NY',

             'streetAddress': '21 2nd Street'},

 'age': 27,

 'children': [],

 'firstName': 'John',

 'lastName': 'Smith',

 'phoneNumbers': [{'number': '212 555-1234', 'type': 'home'},

                  {'number': '646 555-4567', 'type': 'office'},

                  {'number': '123 456-7890', 'type': 'mobile'}]}

>>> 

As you can see, the output is now well formatted and more readable.

What we did is to import the pprint function of pprint module. And use pprint() function rather than the print function:)

Save figure as a pdf file in Python

This post introduces how to save a figure as a pdf file in Python using Matplotlib.

When using savefig, the file format can be specified by the extension:

savefig('foo.png')
savefig('foo.pdf')

The first line of code above will give a rasterized  output and the second will give a vectorized output.

In addition, you’ll find that pylab leaves a generous, often undesirable, whitespace around the image. You can remove it with:

savefig('foo.png', bbox_inches='tight')

You can also use  figure to set dpi of the figure:

import numpy as np
import matplotlib.pyplot as plt

fig  = plt.figure(figsize=(1,4),facecolor = 'red', dpi=100)
plt.savefig('test.png', dpi=100)

plt.show(fig)

 

 

Make a request to REST API using Python

This post introduces how to make a request to REST API using Python.

requests package is the commonly used one (its GitHub repo).

You can try out requests online here at codecademy, and here at runnable.com

Look at this post for a great tutorial using Requests with Python to make a request to REST API: Python API tutorial – An Introduction to using APIs (pdf) – a very good, comprehensive, and detailed tutorial.

To install Requests, simply:

$ pip install requests

See below for a simple example to make a request to REST API.

#Python 2.7
#RestfulClient.py

import requests
from requests.auth import HTTPDigestAuth
import json

# Replace with the correct URL
url = "http://api_url"

# It is a good practice not to hardcode the credentials. So ask the user to enter credentials at runtime
myResponse = requests.get(url,auth=HTTPDigestAuth(raw_input("username: "), raw_input("Password: ")), verify=True)
#print (myResponse.status_code)

# For successful API call, response code will be 200 (OK)
if(myResponse.ok):

    # Loading the response data into a dictionary variable
    # json.loads takes in only binary or string variables so using content to fetch binary content
    # Loads (Load String) takes a Json file and converts into python data structure (dictionary or list, depending on JSON)
    jData = json.loads(myResponse.content)
    #jData = json.loads(myResponse2.content, 'utf-8') #use this line if your data contains special characters

    print("The response contains {0} properties".format(len(jData)))
    print("\n")
    for key in jData:
        print key + " : " + jData[key]
else:
  # If response code is not ok (200), print the resulting http error code with description
    myResponse.raise_for_status()

 

======working with JSON data

For example, if data.json file looks like this:

{
 "maps":[
         {"id":"blabla","iscategorical":"0"},
         {"id":"blabla","iscategorical":"0"}
        ],
"masks":
         {"id":"mask-value"},
"om_points":"value",
"parameters":
         {"id":"blabla3"}
}

The python code should be something looks like this:

import json

with open('data.json') as data_file:    
    data = json.load(data_file)
print(data)

We can now  access single values in the json file — see below for some examples to get a sense of it:

data["maps"][0]["id"]  # will return 'blabla'
data["masks"]["id"]    # will return 'mask-value'
data["om_points"]      # will return 'value'

References:

Print multiple variables in Python3

This post introduces several ways to print multiple arguments in python 3.

  • Pass it as a tuple:
print("The cost for %s is %s" % (name, cost))
  • Pass it as a dictionary:
print("The cost for %(n)s is %(c)s" % {'n': name, 'c': cost})
  • Use the new-style string formatting:
print("the cost for {} is {}".format(name, cost))
  • Use the new-style string formatting with numbers (useful for reordering or printing the same one multiple times):
print("The cost for {0} is {1}".format(name, cost))
  • Use the new-style string formatting with explicit names:
print("The cost for {n} is {c}".format(n=name, c=cost))
  • Pass the values as parameters and print will do it:
print("The cost for", name, "is", cost)

If you don’t want spaces to be inserted automatically by print in the above example, change the sep parameter:

print("The cost for ", name, " is ", cost, sep='')
  • Use string concatenation
print("The cost for " + name + " is " + cost)

NOTE: If cost  is an int, then, you should convert it to str:

print("The cost for " + name + " is " + str(cost))
  • Note that %s mentioned above can be replace by %d or %f.

If cost is a number, then

print("The cost for %s is %d" % (name, cost))

If cost is a string, then

print("The cost for %s is %s" % (name, cost))

If cost is a number, then it’s %d, if it’s a string, then it’s %s, if cost is a float, then it’s %f

  • Use the new f-string formatting in Python 3.6:
print(f'The cost for {name} is {cost}')

Read file from line 2 or skip header row in Python

This post introduces how to read file from line 2 in Python.

  • method 1:
with open(fname) as f:
  next(f)
  for line in f:
    #do something

Note: If you need the header later, instead of next(f) use f.readline() and store it as a variable.
Or use header_line = next(f).

  • method 2
f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

This will skip 1 line. for example, [‘a’, ‘b’, ‘c’][1:] => [‘b’, ‘c’]

 

Open multiple files using “open ” and “with open” in Python

This post introduces two ways to open multiple files in Python.

  • “with open” # do not need to bother to close the file(s) if use “with open”
with open("datafile.csv" , "r") as f:
  f.read()
with open("datafile2.csv" , "r") as f2:
  f2.read()

Or

try:
  with open('file.csv', 'w') as f, open('file2.csv', 'w') as f2:
    do_something()
except IOError as e:
  print 'Operation failed: %s' % e.strerror
  • “open”  # need to close the file when use open.
f = open("datafile.csv" , "r")
f2 = open("datafile2.csv" , "r")
f.read()
f2.read()
f.close()
f2.close()

 

References:

 

Remove a character from a string using Python

This post introduces how to remove / replace a character from a string using Python.

In Python, strings are immutable, so we need to create a new string. If you want to remove the ‘;’ wherever it appears. See below for an example.

Remove character”;”

>>> original_str = "good; ok"
>>> new_str = original_str.replace(";","")
>>> print(new_str)
good ok
>>>

See the examples below for different ways to check whether a string contains a specific character.

>>> s = "good; ok"
>>> ";" in s
True
>>> ";" not in s
False
>>> s.find(";") == -1
False
>>> s. find(";") != -1
True
>>> chars = set(";o")
>>> if any((c in chars) for c in s):
>>> print("Found")
Found
>>> import re
>>> pattern = re.compile(r";o")
>>> if pattern.findall(s):
>>> print("Found")
Found
>>>

 

References:

How to delete a character from a string using python?

Remove all special characters, punctuation and spaces from string

How to check a string for specific characters? (pdf)

Create a hash table for large data in python

This post introduces how to create a hash table in python.

Text files can have duplicates which will overwrite existing keys in your dictionary (the python name for a hash table). We can create a unique set of the keys, and then use a dictionary comprehension to populate the dictionary.

sample_file.txt:

a
b
c
c

 

Python code:

with open("sample_file.txt") as f:
  keys = set(line.strip() for line in f.readlines())
my_dict = {key: 1 for key in keys if key}

>>> my_dict
{'a': 1, 'b': 1, 'c': 1}