Python cookbook

Python iterate dictionary

Iterate Python dictionary (associative arrays) Using keys It is not memory efficient for large dictionaries. Using values It is not memory efficient for large dictionaries. read more

Python clone/copy array

Python cloning an array (list) using slice operator.

Python merge two lists (arrays)

To merge two (or more) lists (arrays) the operator plus (+) can be used. It will append items of 2nd list to first list and read more

Python string to int

Python string can be converted to int using int(strval). Note that it will throw ValueError for invalid string. Here is a quick example to convert read more

Python int to string

Python int (or any other data type like float) needs to be converted to string before we can do concatenation operation using plus (+). It read more

Python dictionary basics

Python dictionary (associative arrays) – some basic operations. Initialize dictionary Check if a key exists access value for a key list length/size using len() len() read more

Python initialize large list with same value

To initialize a large list with same value (all items having same value), the following syntax can be used. a = [val] * num_items Example

Python array slice

Slicing a list (array) in python is very handy way to get a sub list of an array. Its syntax is ## slice from start read more

Python list/array – remove an item

To remove an item from python list method pop([i]) can be used. Remove last item Remove first item Remove ith item

Python list – append or prepend a value

Appending or prepending a value to a list in Python. Append value Prepend value

Python list (array) basics

Python list (or array) – some basic operations. Initialize list access ith item access last item list length/size using len() Note than len() can also read more

Python string contains check

To check if a string is part of another string (case sensitive) we can use these approaches Using in operator Using string find() method ## read more

Python string lowercase and uppercase

To convert a string to lowercase of uppercase following code can be used: s_lower = s.lower() s_upper = s.upper() string lowercase example string uppercase example

Python if-else and if-elif-else

Python if else statements can be expressed on two ways Simple if-else statement if-elif-else statement with one or more occurrences of elif. if else example read more

Python etl petl – read table from csv file

Python elt library petl can be used to perform extract/load – reading/writing tables from files and databases. In this tutorial we’ll read a table in read more

Python file read write examples

Reading and writing files in python is very common use case. Here are some examples: Write to file using “with open()” Here file gets closed read more

Python value in array (list) check

In Python we frequently need to check if a value is in an array (list) or not. Python x in list can be used for read more

Python string split examples

Python has in-built string split function which can be used to split a string on any delimiter and return a python list (array). If no read more

Python string quick start tutorial

Python built-in data type string is very frequently used in any python program. Here are some examples of using strings to get started with. Print read more

Python – dictionary get key with default

Get dictionary key with default if not found using method get() This is better approach in most cases. Get dictionary key using square brackets ([]) read more

Python for loop examples

Frequently used python for loop examples. For loop to iterate over a list Iterate on each element of a list Iterate on each index, element read more

How to find python package’s file location

Here is simple python code snippet to see the file location of a python package on command line: $ python -c “import urllib2; print urllib2.__file__;” read more

Command line – run python webserver

Sometime we need to run a quick static webserver which can serve various files in a directory. This can be pretty handy to serve html read more

Python – How to sort dictionary by values

Sometimes we want to iterate over a dictionary in sorted order of values. We can do it by using key argument to python sorted() method read more