Beautiful Python: Some Cool Language Constructs and Tricks for Beginners - Part 1

0. Pretty printing of a dictionary
Suppose you have a nested dictionary and you want a human readable view of it, you could use json module to accomplish this. json.dumps() takes an extra parameter 'indent' that formats the elements legibly.

>>> import json

>>> a = { 'a': {'b': {'c':'d', 'e':'f'}}}

>>> print json.dumps(a, indent=2)
  "a": {
    "b": {
      "c": "d",
      "e": "f"

You can also use the pprint python module for pretty print of python data structures.

1. Reverse an iterable in python

>>> a = [1, 2, 4]

>>> a[::-1]
[4, 2, 1]

>>> a
[1, 2, 4]

>>> b = (2, 3, 4)

>>> b[::-1]
(4, 3, 2)

>>> b
(2, 3, 4)

>>> c = "This is a string"

>>> c[::-1]
'gnirts a si sihT'

>>> c
'This is a string'
This method always returns a new instance of the iterable instead of an in-place reverse.

2. Swapping the values of two variables in python
>>> a = 1

>>> b=2

>>> a,b = b,a

>>> a

>>> b

How does this work?
Python separates the right-hand side expression from the left-hand side assignment. First the right-hand side is evaluated, and the result is stored on the stack, and then the left-hand side names are assigned using opcodes that take values from the stack again.
For tuple assignments with 2 or 3 items, Python just uses the stack directly:
>>> import dis
>>> def foo(a, b):
...     a, b = b, a
>>> dis.dis(foo)
  2           0 LOAD_FAST                1 (b)
              3 LOAD_FAST                0 (a)
              6 ROT_TWO             
              7 STORE_FAST               0 (a)
             10 STORE_FAST               1 (b)
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE        
After the two LOAD_FAST opcodes (which push a value from a variable onto the stack), the top of stack holds [a, b]. The ROT_TWO opcode swaps the top two positions on the stack so the stack now has [b, a] at the top. The two STORE_FAST opcodes then takes those two values and store them in the names on the left-hand side of the assignment. The first STORE_FAST pops a value of the top of the stack and puts it into a, the next pops again, storing the value in b. The rotation is needed because Python guarantees that assignments in a target list on the left-hand side are done from left to right.
For rest of the answer refer:

3. Enumerate
When you loop through a sequence or an iterable, you can get the index and its corresponding value at the same time by wrapping the sequence in enumerate.

>>> for index, value in enumerate(['foo', 'bar', 'zoo']):
...     print index, value

0 foo
1 bar
2 zoo

4. Splitting a string into a list of words and join them back

To split a string by whitespace
>>> a = "This is a string"

>>> a.split()

['This', 'is', 'a', 'string']

To split a string by a character
>>> a = "This is a string"

>>> a.split('s')

['Thi', ' i', ' a ', 'tring']

To join a list of words by space
>>> b
['This', 'is', 'a', 'string']

>>> " ".join(b)
'This is a string'

To join a list of words by a character, comma for example

>>> b
['This', 'is', 'a', 'string']

>>> ",".join(b)

5. List Comprehensions
Suppose you have a list of elements and you need to do some operation on each of the element. For example, you have a list L consisting of words each of length greater than 5 and you have to create a new list consisting of first three letters of each word in L.
The common way to write code for this would be:

>>> L = ["Python", "makes", "people", "love her"]

>>> new_L = []

>>> for word in L:
...     new_L.append(word[0:3])

>>> new_L
['Pyt', 'mak', 'peo', 'lov']

This is where List comprehensions come to the rescue

>>> L = ["Python", "makes", "people", "love her"]

>>> new_L = [word[0:3] for word in L]

>>> new_L
['Pyt', 'mak', 'peo', 'lov']

This effectively reduced the number of lines from 3 in earlier approach to 1 using List comprehensions. Also, generally, List comprehensions are considered to be faster and efficient than creating an empty list and appending an element to that list one by one.

Now suppose you need only the words from L which have length>5.
>>> L = ["Python", "makes", "people", "love her"]

>>> new_L = [word for word in L if len(word)>5]

>>> new_L
['Python', 'people', 'love her']

Further reads on List Comprehensions:

Crawling a Website that loads content using Javascript with Selenium Webdriver in Python

Selenium is a browser automation tool that is used primarily for testing web applications. You can simulate real user actions and interactions with your web applications. Selenium supports all the major browser platforms  and operating systems. There are bindings for all the popular programming languages. The power of Selenium is not just restricted to testing your web apps, one other use can be of crawling or scraping websites, in particular, the ones which don't provide an API and load content lazily using Javascript.

Today, we will be crawling an online merchant website with Selenium using its python bindings. Jabong loads more products as you scroll down a web page. We will use Selenium to simulate this user action of scrolling down a web page and then retrieving all the product titles and the corresponding links to the product detail pages.


Platform: Ubuntu 12.04

Tools Required

sudo pip install selenium

sudo pip install xvfb

sudo pip install pyvirtualdisplay

We use pyvirtualdisplay which is a wrapper around xvfb and enables you to run Firefix headlessly.

Page to Crawl:

A quick "Inspect Element" on a shoe above shows that each of the product is wrapped by a "div" element with class "hover-box" and the title and links are embedded in an "a" element within those "div" elements.


Nike Ballista Iv Msl Grey Running Shoes
U.S. Polo Assn. Navy Blue Sneakers
Nike Dewired Navy Blue Sneakers
United Colors of Benetton Black Boat Shoes
Phosphorus Brown Loafers
U.S. Polo Assn. Delta Beige Sneakers
Asics Kayano 20 Black Running Shoes
Nike Air Max 2014 Blue Running Shoes
Asics Excel 33 3 Navy Blue Running Shoes
Nike Air Relentless 3 Msl Blue Running Shoes
Asics Kayano 20 Navy Blue Running Shoes
Nike Lunarinternationalist Grey Running Shoes
Nike Free 5.0+ Blue Running Shoes
Nike Fs Lite Run Black Running Shoes
Nike Lunarinternationalist Blue Running Shoes
Nike Free Trainer 5.0 Grey Running Shoes
Andrew Hill Brown Dress Shoes
Nike Lunar Forever 3 Msl Grey Running Shoes
Asics Kayano 20 Red Running Shoes
Nike Free Trainer 5.0 Black Running Shoes
Nike Chroma Thong Iii Green Slippers
Nike Aquahype Blue Flip Flops
Z Collection Green Loafers
Nike Flex 2013 Rn Black Running Shoes
U.S. Polo Assn. Brown Sneakers
Phosphorus Black Loafers
Phosphorus Black Loafers
Nike Eliminate Ii Leather Grey Sneakers
Nike Fs Lite Trainer Blue Running Shoes
Nike Suketo 2 Leather Red Sneakers
Nike Free Flyknit+ Red Running Shoes
Nike Air Pegasus+ 30 Grey Running Shoes
Nike Flex Supreme Tr 2 Grey Running Shoes
Phosphorus Black Loafers
Phosphorus Black Loafers
Phosphorus Black Loafers
Nike Zoom Structure+ 17 Black Running Shoes
Nike Flyknit Lunar2 Black Running Shoes
Phosphorus Brown Loafers
Andrew Hill Black Dress Shoes
Phosphorus Tan Loafers
Phosphorus Black Loafers
U.S. Polo Assn. Delta Navy Blue Sneakers
Phosphorus Brown Loafers
Nike Lunar Forever 3 Msl White Running Shoes
Asics Kayano 20 White Running Shoes

Resources for Learning Number Theory for Competitive Programming

Here are few resources for learning number theory for competitive programming.

Online resources:
1. Stanford Course: CS 97SI: Introduction to Competitive Programming Contests
2. Codechef: Basic Number Theory Every Programmer Should Know..
3. Topcoder: Primality Testing: Non Deterministic Algorithms
4. Topcoder: Prime Numbers, Factorization and Euler Function
5. MAXimal Russian Site. Contains the implementations of all major algorithms used in competitive programming contests

1. Elementary Number Theory by David Burton
    DescriptionElementary Number Theory, Seventh Edition, is written for the one-semester undergraduate number theory course taken by math majors, secondary education majors, and computer science students. This contemporary text provides a simple account of classical number theory, set against a historical background that shows the subject's evolution from antiquity to recent research. Written in David Burton’s engaging style, Elementary Number Theory reveals the attraction that has drawn leading mathematicians and amateurs alike to number theory over the course of history.

2. Number Theory for Computing
     DescriptionThis book provides a good introduction to the classical elementary number theory and the modern algorithmic number theory, and their applications in computing and information technology, including computer systems design, cryptography and network security. In this second edition proofs of many theorems have been provided, further additions and corrections were made

Top Sites to learn and practice programming

SingPath is the most FUN way to practice software languages! SingPath provides a platform to those that want to test their programming skills in a competitive and fun environment. There is a range of programming languages that you can test your skills with including Python, JavaScript, Ruby, Objective C and Java with many other languages to come. Specialize in one language or show your flexibility and skill by coding in multiple languages.


Add caption



Khan Academy




How to take set difference of two sets in C++?

Set difference of two sets A and B is defined as the set C consisting of elements from A not present in B.

In C++, algorithm header in STL defines a function set_difference which in conjunction with inserter can be used to take set difference of two sorted range of elements.




using namespace std;


set<int> A, B, C;


set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.end()))

So, this basically takes the set difference of the whole of A and B and copies those elements to C.

set_difference can even work on vectors and arrays. Just make sure that the vectors and arrays are sorted before you take the set difference.

Complexity: O(n)

How to convert a byte array into a string in C#?

In applications where you have binary data in a byte array for e.g. data received via a SocketStream, and you need to convert it to a string to interpret it, here is how you do it:
string result = System.Text.Encoding.UTF8.GetString(byteArray)