Posts Tagged ‘python’

A Tour On Python List

Posted: May 28, 2015 in Programming
Tags: , , ,

 For those who have used Arrays in C and C++, list in Python is going to be a lucky charm . As Head First Python states, List is nothing but an array with steroids. Python list makes the life very easier as its dynamic in nature and the in-build functions are available to perform all the actions on it.

First lets list down the basic stuffs that we always do on arrays .

1. Declaring
2. Creating
3. Initializing with predefined input
4. Adding More input to it
5. Searching
6. Indexing
7. Removing
8. Erasing
9. In Built Functions

In this tutorial, am going to use 2 lists. One named numbers and other named alphabets. Lets go and see one by one .

1. Declaring a list

As we know, we need not declare a variable with appropriate data type in python . both a = 5 and a = ‘5’ are valid without specifying the corresponding data type. And more importantly, its applicable to lists as well.

2. Creating a list

When i say creating , it has 2 modes. Creating an empty one to add items later. Or creating a predefined set of elements in the list. To create an empty list, all we need to do is to write an open and closed square brackets.

numbers = []

3. Initializing with Predefined Input:

Sometimes, we will be really sure that we are going to work with only some specific inputs. In those situations we can create a list with pre defined inputs in it. That can be done by enclosing the inputs inside the square bracket

numbers = [1,2,3,4]

4. Adding more input to a List:

Adding input to the list can be categorized as following

4.1 – Adding elements one by one to a list
4.2 – Adding elements in a block
4.3 – Multiplying the list
4.4 – Adding list inside a list

4.1 – Adding elements one by one to the list :

Python has predefined function named “insert” , which can be used to add elements to the list. However, the insert takes two arguments. first one is the position and second one is the value. The list with numbers 1,2,3,4 can also be created as below

numbers = []

4.2 – Adding elements in block

If suppose we have 2 lists. first one containing numbers 1 to 3 and other one containing numbers 4 to 6. And we need to merge two list. ie. adding elements in block, then we can do it as below:

num1 = [1,2,3]
num2 = [4,5,6]
num1 + num2

or simply

[1,2,3] + [4,5,6]

4.3 – Multiplying the list

Python creates a easy way of inserting repeated items. For example, if we need a list which has 3 numbers 1,2,3 ten times. we need not write a for loop . Just a simple multiplication operator on list will do it as below

numbers = [1,2,3]
numbers * 10

4.4. Nesting Lists

Yes, we can have list inside a list. And there is a specific way for indexing the list. In the list below , 4th item is a list which contains 3 items in turn.

numAlpha = [1,2,3 [ “a”, “b”, “c” ]]

5. Searching an element in the list

If we need to check an element exists in the list , we can use “in” keyword with list which will return a boolean value (True indicating the element exists and false indicating the element doesnt exist in the list)

numbers = [1,2,3,4,5,6]
4 in numbers
7 in numbers

6. Indexing list

We can get the element based on the index of that element in the list. Since, all the elements are inserted into the list with the index element, we can derive any element if we know the index. Index starts with 0 and moves on as we insert the elements.

List_name[start : end]

This is the common notation . start denotes the starting index that we need to start with, to start from beginning give 0 or leave it empty. End denotes the position to which list should print

numbers = [1,2,3,4,5,6]
numbers[1:4] - show elements from index 1 till 3
numbers[:2] - show elements from beginning till 1st index
numbers[2:] - show elements from index 2 to end of the list
numbers[:] - print all elements
numbers[-2] - tricky one .. show 2nd last element in the list

7. Removing elements from a list :

We can remove an element from the list if we know the index or element value. remove function can be used if we know the list value and del function can be used to remove using index.

numbers = [1,2,3,4,5,6]
del numbers[-4]

8. Erasing the list :

If we need to erase all the elements in the list , we can use clear function . List_name.clear() will remove all the elements from list.

numbers = [1,2,3,4,5,6]

9. Inbuilt Functions :

There are lot of inbuilt functions available in python list. One that we saw is len(list_name) which gives the length . Similarly we have

len – finds the total number of elements in the list
count – finds the number of occurrence for an element
sort – sorts the list
reverse – reverse the list
index – returns the index of first matching element

Am completely new to python and Django . Today i made an attempt of installing Django in my ubuntu 14.04 system.

The OS is fresh installed, just did 4 days back . Django is going to be my first package installation.

I went with the Official Documentation given below

Official Documentation for Django Installation

As given in the link , there are 3 major steps.

1. Check if python is already installed in your computer.

I did by typing

python –version in my terminal and i got answer as Python 2.7.6.

Hence, i proceeded to step 2 .

2. Remove any previous versions of Django if installed.

sudo rm -rf /usr/lib/python2.7/dist-packages/django

You have to run this command to remove the previous version. Change the python version accordingly in the command

If you skip this step you might be getting an error as given below

OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/django'

I tried installing Django Directly using pip which gave me this error . And i visited the following URL for the solution.

Solution for OS Error

3. Install Django.

In ubuntu 14.04 LTS i didnt have pip installed in prior. Hence i had to install that first. That can be done by

sudo apt-get install python-pip

and then you can install Django by typing this command

sudo pip install Django==1.8.1

Django installed successfully.

To verify open the python prompt by typing python in the terminal and use the following commands to see Django version

>>> import django
>>> print(django.get_version())

Very new to python Programming . I tried installing python 2.7 in my linux machine (ubuntu). Unfortunately , python 3 was already installed in the system.

For parsing the twitter tweets using python, i had to install oauth2 in my machine. i spent almost 2 to 3 hours doing it . Got almost 5 different kind of packages from internet and tried installing it.

Also installed additional libraries. PIP and all stuffs . But still, it didnt work. I got the same error as below

Traceback (most recent call last):
File “”, line 1, in
import oauth2 as oauth
ImportError: No module named oauth2

At last , i found that the error was occuring because of the 2 differnt python versions in my system.

Then i started locating python directory and checked where it is pointing to. when i checked /usr/bin/python , it was pointing to python 2.7.

Instead of running the script as


i changed it to


And yes, it worked. Sometimes we complicate few easy stuffs 🙂