Python array tutorials and examples

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 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 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 xrange examples

Python xrange can be used to generate a sequence of numbers with desired start number and gap. It is often used in for loops. It read more

Python range examples

Python range can be used to generate a range of numbers with desired start number and gap. It is often used in for loops. Usage: read more

Python itertools imap examples

Python itertools imap is efficient way of mapping and looping over a large iterable. Here is the usage for imap: itertools.imap(function, *iterables) The function should read more

Python itertools ifilter examples

Python itertools.ifilter is efficient way of filtering and looping over a large iterable. Here is the usage for ifilter: itertools.ifilter(function, iterable) The function should take read more

Python map examples

Python map is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that map applies 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 filter vs ifilter

Python filter is very handy python built-in function to filter iterables (e.g. lists) in python. But it returns a list which may consume huge memory read more

Python filter list/iterable examples

Python filter is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that filter applies 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