Monday, March 5, 2012

Python Lists: How to reverse a list ?

Ok here is the code snippet


>>> b=['13', '18', '22', '47', '51', '64', '76', 'Mike', 'Reetu', 'apple', 'orange']
>>> b.reverse()
>>> b
['orange', 'apple', 'Reetu', 'Mike', '76', '64', '51', '47', '22', '18', '13']

Python List: How to sort items in list ?

Ok, Now that you have a list, you might have to sort it too sometime. Python lists are amazing for sorting, you can sort very easily even if it contains items of different data types.

Sort list with items of same data type

>>> b=['76','22','18','64','51','47','13']
>>> b.sort()
>>> b
['13', '18', '22', '47', '51', '64', '76']




Sort list with items of different data type. You can see first it sorts number followed by sorting words with capital initial letter . then followed by sorting of all small letter.

>>> b=['76','22','18','64','51','47','13','orange','apple','Reetu','Mike']
>>> b.sort()
>>> b
['13', '18', '22', '47', '51', '64', '76', 'Mike', 'Reetu', 'apple', 'orange']


Python Lists: How to delete items ?

Okay guys here is a simple guide if you are trying to delete some items from the python list.

1. Delete item at index i (lets say delete item at index 2)

>>b=['16','22','38','44']
>>> b[2:3]=[]
>>> b
['16','22','44']





2. Delete item whose value is val

>>b=['16','22','38','44']
>>b.remove(22)
>>b
['16','38','44']





3. Delete range of items from index k to l (lets say from 3 to 6)

>>b=['16','22','38','44','51','67','73']
>>> b[3:6]=[]
>>> b
['16', '22', '38', '73']





4. Delete all items from a list

>>b=['16','22','38','44','51','67','73']
>>> b[:]=[]
>>> b
[]



Thursday, March 1, 2012

Python Lists: How to replace items

Lets say we have a list L.

>>> L=[11,12,13,14,15,16,17,18,19]

Note: list which is result of previous step, command is run on this resulting list in next step unless explicitly specified.

Replace a single item at index k(k=2):

          It means replace item with value 13.


          >>> L[2]='thirteen'

          We successfully replaced 13 with 'thirteen'.
          >>> L
          [11, 12, 'thirteen', 14, 15, 16, 17, 18, 19]


          Lets replace 11 by 10

          >>> L[0]=10

          >>> L
          [10, 12, 'thirteen', 14, 15, 16, 17, 18, 19]


          Lets replace it back to 11,


          >>> L[0]=11
          >>> L
          [11, 12, 'thirteen', 14, 15, 16, 17, 18, 19]




          Replace a range of items(replace items at 3 and 4) by a single item:


          >>> L[3:5]=[1415]  (Note: range goes from 3 to 5 to replace 3,4)
          >>> L
          [11, 12, 'thirteen', 1415, 16, 17, 18, 19]


          NOTE: When replacing a range of items, you DONT DO following:

          >>> L[3:5]=1415 

          OR

          >>> L[3:5]='1415'

          Well you can do it and see what happens if you do it.


Replace a single item by another list:

          Lets replace items at index 3 with value 1415 in above list back to 14,15


          >>> L[3:4]=[14,15]
          >>> L

          [11, 12, 'thirteen', 14, 15, 16, 17, 18, 19]



Replace a range of items by another list:

          So our list looks like following right now after doing step just described above.

          >>> L
          [11, 12, 'thirteen', 14, 15, 16, 17, 18, 19]


          Now we want to replace items at index 3,4,5(writing range goes 3 to 6)  by another list ['dog','cat']



          >>> L[3:6]=['dog','cat']
          >>> L
          [11, 12, 'thirteen', 'dog', 'cat', 17, 18, 19]




Replace all items after index k:

          Ok, now we want to replace all the items after index 4.
          5th index onwards I want to replace all items.



          >>> L[5:]=['rat','duck']
          >>> L
          [11, 12, 'thirteen', 'dog', 'cat', 'rat', 'duck']



Replace all items before index k:

          So our L looks like following

          >>> L
          [11, 12, 'thirteen', 'dog', 'cat', 'rat', 'duck']

          We want replace all items before index 2. i.e. replace 0,1.


          >>> L[:2]=['kitten','maggie','apple']
          >>> L
          ['kitten', 'maggie', 'apple', 'thirteen', 'dog', 'cat', 'rat', 'duck']




Replace all items of L:


          Currently our list looks like following:


          >>> L
          ['kitten', 'maggie', 'apple', 'thirteen', 'dog', 'cat', 'rat', 'duck']




          >>> L=[11,12,13,14]
          >>> L
          [11, 12, 13, 14]

          OR


          >>> L[:]=[11,12,13,14]
          >>> L
          [11, 12, 13, 14]


NOTE: Of course you can replace the items with just empty list.

So I replace item at index 1, with an empty list.


          >>> L[1:2]=[]
          >>> L
          [11, 13, 14]

Lets  recreate L with

          >>> L=[11,12,13,14]


Now replace range of items(1 to 3: means replace only 1,2  ; 3 is not included.)


          >>> L[1:3]=[]
          >>> L
          [11, 14]



Lists: How to add/insert item

I am assuming, you have started python on Terminal by now.(Not yet, HERE is HOW ? )
Right now we have an empty list

          >>> a = [ ]


Lets ADD some items to it

          >>> a.append('cat')
          >>> a.append(99)

Note: item doesn't necessary have to same be of same data type, as you can see one of them is string and another one is int.

You want to see how our list a looks like, lets print

          >>> a
          ['cat', 99]


So now our list have 2 items in it, 'cat' and 99.



Now lets INSERT some items in it at index k(k+1 th position from the front, the first item sits at  index 0).

I want to insert 'dog' after 'cat'

          >>> a.insert(1,'dog')

So, lets see what does our list looks like now,

          >>> a
          ['cat', 'dog', 99]

We successfully inserted 'two' at index 1 !!




------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

There are intact some other ways too to perform ADD/INSERT action.

We can INSERT the items array style, lets insert some items then.

Lets say our array is

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

Now insert 0.5 at index 1. so that array looks like [0, 0.5,1, 2]


          >>> a[1:1]=[0.5]
          >>> a
          [0, 0.5, 1, 2]

You can insert list of more than 1 items too, lets add some more fractions at index 1.


          >>> a[1:1]=[0.1,0.25]
          >>> a
          [0, 0.1, 0.25, 0.5, 1, 2]



So as you can see, this is basically a LIST INSERTION, which contains only 1 item.
[0.5] is a list of 1 item only which has been inserted at index 1 in list a.



Lets do array style addition.

Lets say now we have a list b.

          >>> b = [ ]

We can't really add items in array style to this empty list in following format

          >>>b[0]=0  
          >>>b[1]=1  

But if you already have some items in list b, then you can ONLY REPLACE items at that index in array style , we will see that later in other post under "How to replace items in list."








Lists: How to create list

List can contain items of different data type.

Create list with hard coded items :  >>> a = ['one', 'two', 1, 2]


Create empty list which is mutable by default  >>> a = [] , yes this is how you init a list.


You can now add/append/insert in this list , we will see that in next post.

How to run python script as file on Mac ?

Tired of writing the python code on terminal line by  line, it is time that you want to write in one file and then run it.
For that you create your script on a text file and save it as say myscript.py

Now start terminal (HOW ?) and write python myscript.py
As a result it will give you any errors and its respective line number where that error is present if not then you can see your print /logged lines of the code on terminal.