Saturday, March 10, 2012

Slicing with Lists

In in this Post I will show you how to play with Lists by Slicing. The basics of Slicing were given in the earlier posts, If you are not sure what Slicing is, click here Slicing

As usual open up the IDLE and create a List.

>>> python=list('Pythonishard')
>>> python
['P', 'y', 't', 'h', 'o', 'n', 'i', 's', 'h', 'a', 'r', 'd']
>>>

Ok so we have create a list and assigned it to variable python.

Now we will see how to edit this list, this list says Pythonishard, but I dont think so, and i will change it to Pythoniseasy, lets see how..

>>> python=list('Pythonishard')
>>> python
['P', 'y', 't', 'h', 'o', 'n', 'i', 's', 'h', 'a', 'r', 'd']
>>> python[8:]='easy'
>>> python
['P', 'y', 't', 'h', 'o', 'n', 'i', 's', 'e', 'a', 's', 'y']
>>>

Here we wanted to change the characters from index number 8, so python[8:0] means start overwriting from index number 8 and go till end, as the end index number is not specified.

Now lets see how to add elements to the end of the list.

>>> len(python)
12
>>>

So the Length of the List is 12, so we need to start adding from the index number 12.

>>> python[12:]=', lets start'
>>> python
['P', 'y', 't', 'h', 'o', 'n', 'i', 's', 'e', 'a', 's', 'y', ',', ' ', 'l', 'e', 't', 's', ' ', 's', 't', 'a', 'r', 't']
>>>

Now lets see how to insert elements at a certain place. lets create another List.

>>> test=list('GodGreat')
>>> test
['G', 'o', 'd', 'G', 'r', 'e', 'a', 't']
>>> test[3:1]='is'
>>> test
['G', 'o', 'd', 'i', 's', 'G', 'r', 'e', 'a', 't']
>>>

So we wanted to insert 'is' before 'Great' index number of G is 3, so we need to to find the index number of the element before which we need to insert, so test[3:1]='is' means add 'i', 's' before index number 3 and 1 means from index 3 start in count of 1.

Now lets see how to delete elements from a List, we can very well do with the function del(index num), but here is one more way.


Lets create another list

>>> whiskey=list('PythonScript')
>>> whiksey

Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    whiksey
NameError: name 'whiksey' is not defined



Its so embarrassing, i spelled the variable wrong and python gave me an error, sorry about that
>>> whiskey
['P', 'y', 't', 'h', 'o', 'n', 'S', 'c', 'r', 'i', 'p', 't']

Okay lets say we want to delete 'c','r','i','p'

So if we count we see that it 'c' starts with index number 7

So what we can do is:

>>> whiskey
['P', 'y', 't', 'h', 'o', 'n', 'S', 'c', 'r', 'i', 'p', 't']
>>> whiskey[7:11]=[]
>>> whiskey
['P', 'y', 't', 'h', 'o', 'n', 'S', 't']
>>>

whiskey[7:11]=[], means start from index number 7, and delete everything till index number 10(One less than the defined)

Okay so now we got some more information about how to play with Listing by Slicing. I Hope This was Informative For You.

Here is a Task For you, Make a List, and use all the functions described in this post.

Thank You for Reading this and Don't forget to check my next post.

0 comments:

Post a Comment