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 = []

https://shrinathsuccess.files.wordpress.com/2015/05/list2.png?w=614

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]

https://shrinathsuccess.files.wordpress.com/2015/05/list3.png?w=614

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 = []
numbers.insert(0,1)
numbers.insert(1,2)
numbers.insert(2,3)
numbers.insert(3,4)

https://shrinathsuccess.files.wordpress.com/2015/05/list4-1.png?w=614

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]

https://shrinathsuccess.files.wordpress.com/2015/05/list4-2.png?w=614

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

https://shrinathsuccess.files.wordpress.com/2015/05/list4-3.png?w=614

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” ]]

https://shrinathsuccess.files.wordpress.com/2015/05/list4-4.png?w=614

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

https://shrinathsuccess.files.wordpress.com/2015/05/list5.png?w=614

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

https://shrinathsuccess.files.wordpress.com/2015/05/list6.png?w=614

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]
numbers.remove(5)
numbers
del numbers[-4]
numbers

https://shrinathsuccess.files.wordpress.com/2015/05/list7.png?w=614

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]
numbers.clear()
numbers

https://shrinathsuccess.files.wordpress.com/2015/05/list8.png?w=614

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

https://shrinathsuccess.files.wordpress.com/2015/05/list9.png?w=614

The firefox update can be downloaded as *.tar.gz from the official website.

We can directly move to the downloaded directory and untar it . It will give us a folder named firefox .Inside that , we will be having an executable named firefox. Once you trigger the executable , the new version of firefox will launch.

But, we have a problem here. The existing version still exists in home folder as .firefox . And if you take an attempt by clicking on the firefox icon, it will launch the older version of your firefox.

We need to find out the directory where the firefox is installed. In my case, it was in /usr/lib . Sometimes, it may get installed in /opt or /etc. we need to replace the /usr/lib/firefox directory with our downloaded latest firefox.

cd /usr/lib
sudo cp -r /home/user/download/firefox .

Enter the password to proceed with the action.

After the copy, when you click on the firefox icon , new version of the firefox will open.

One of the best rubik Timer with lot of inbuilt options. I took an attempt of using Prisma Puzzle Timer in Ubuntu 14.04.

We can download the jar from this website
Prisma Puzzle Timer

After downloading we can run the timer from terminal using the following command

Move to the downloaded jar file location

cd /home/user/Downloads

and run the jar file by

java -jar PrismaPuzzleTimer0.6.jar

which will invoke the timer .

But everytime , we need to type this command in the terminal to invoke it .

Instead we can create a laucher file which will invoke this timer. For that we need a simple software .

Alacarte.

We can download the software using the command

sudo apt-get install alacarte

Once downloaded invoke alacarte from the terminal by typing its name

alacarte

and click on new item button to create a new launcher.

In the command type java -jar PathToTheJarFile

in my case its java -jar /home/user/Software/PrismaPuzzleTimer.jar

Save and close . With the given Name (in my case its “RubikTimer”) it will be added to the main menu.

You can search for the work RubikTimer in the menu search tab and launch the software by clicking on the file .

Please refer to the images below .

Installing Alacarte

Setting Up The Launcher Icon

Searching for the Launcer

Prisma Puzzle Timer

I had Ubuntu 12.04 and Windows 7 running in my system and Ubuntu 12.04 got Crashed .I tried debugging it but couldnt succeed.

I decided to do a fresh install of latest version of Ubuntu and that was Ubuntu 14.04. I created a live USB and installed it in my system .

Installation was quick and easy. But , since i created a new Ubuntu install rather than overridding the existing one , Only 9 GB of space was left with me in the system Where ubuntu 14.04 got installed.

I had access to Ubuntu 12.04 Files as a separate drive, which i doesnt needed. I was looking for some application like Disk Management in windows which would help me to resize my partitions and i edned up with Gparted.

It looked pretty complex stuff for me to delete the partition and free up the space . Luckily , dual boot helped me to do it via Disk Management utitility in windows. I had free space ready with me .

Follow the steps below

1. Open Gparted and authorize it.
2. Right click on the partition which you want to increase
3. Select Resize/Move
4. Enter correct value in New size (It should be less then total unallocated space)
5. Click on start button to complete the action.

I was trying to resize my ubuntu 14.04 partion space using Gparted , where i deleted a partition which had my previous ubuntu install.

Unfortunately, the grub was loading from that partition and after my restart the computer went to the promt with grub rescue menu.

If you face the simillar situation , you can recover your OS grub loader as below.

This link was really helpful for me to boot my ubuntu installation .
http://askubuntu.com/questions/493826/grub-rescue-problem-after-deleting-ubuntu-partitionhttp://askubuntu.com/questions/493826/grub-rescue-problem-after-deleting-ubuntu-partition

Follow the steps given by Amr Amyan.

For me the commands were

set root=(hd0,msdos6)
set prefix=(hd0,msdos6)/boot/grub
insmod normal
normal

where msmod6 is the partition where ubuntu resides. That can be found by ls command .
Try ls(hd0,msdosX) and hit enter. you should be a valid partition . (ext2 partition)

But its not the end. You have just booted up the OS. When you restart you will be back to your grub rescue menu.

So here we have 2 options.

1. Using Live CD or Live USB
2. Using Existing Ubuntu

Since my Ubuntu 14.04 got booted up , i installed boot-repair from terminal by following these commands

sudo add-apt-repository ppa:kranich/cubuntu
sudo apt-get update
sudo apt-get install -y boot-repair && boot-repair

Once its done , open boot repair and select “Recommended Repair” Menu . System will recover Grub and give you a link as well.
NOte down the link and restart your computer.

Bingo !!!!

Even after boot repair you are not getting the boot menu, paste the link to the corresponding email id. Ubuntu support team will help you out !!!.

For developing applications in Django, i needed a good IDE for writing code in python and Pycharm is the IDE that works like a charm.

I downloaded the pycharm tar ball package from here.

Download Pycharm from here

and extract the package as

tar xvf Pycharm*.tar.gz

You got the package untarred but, we gotta check one more stuff.

Pycharm will look for JAVA HOME . Check if java is installed .

java -version

if not , install it with

sudo apt-get install default-jre

And run the pycharm.sh script from bin folder.

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())