List in Python
List in Python
List in Python
16bss031
Lists in Python language can be compared to arrays in Java but they are different
in many other aspects. Lists are used in almost every program written in Python.
Defining a List in Python is easy. You just need to provide name of the list and
initialize it with values. Following is an example of a List in Python :
>>> myList
So you can see that a list named ‘myList’ was created with some elements.
Lists in python are zero indexed. This means, you can access individual elements in
a list just as you would do in an array. Here is an example :
>>> myList[0]
'The'
>>> myList[4]
'sun'
Lists cannot be accessed beyond the rightmost index boundary. Here is an
example :
>>> myList[5]
So we see that when we tried to access myList index 5, an error was thrown saying
that this index is out of range.
But, on the other hand, if you try to access value at a negative index then values
from the rightmost index can be accessed backwards. Here is an example:
>>> myList[-1]
'sun'
So we see that index -1 was translated into index ‘4’ and corresponding value was
produced in output.
If you are new to Python, you should start by writing a Python hello world
program, and understanding Python variables and strings.
2. Add Elements to a List
One can use the method insert, append and extend to add elements to a List.
The insert method expects an index and the value to be inserted. Here is an
example of insert :
>>> myList.insert(0,"Yes")
>>> myList
So we see the value ‘yes’ was inserted at the index 0 in the list and all the other
elements were shifted accordingly.
The append method can take one or more elements as input and appends them
into the list. Here is an example :
>>> myList
So, the two values were appended towards the end of the list. Note that whether
we add one element or multiple elements to the list, if we have used append then
they will be added as a single element only. This can be proven if we try to check
the length of myList now :
>>> len(myList)
7
So we see that though we added two elements but they are counted as a single
element (or a sub-list) in myList.
The other method that can be used to add elements to list is extend. Like append,
it also expects one or more values as input. But, unlike append, all the elements
are added as individual elements. Here is an example :
>>> myList
['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']
>>> len(myList)
10
So we see that each element got added as a separate element towards the end of
the list.
Python also allows slicing of the lists. You can access a part of complete list by
using index range. There are various ways through which this can be done. Here
are some examples :
Note that the index 4 was passed instead of 3 because if we pass index range x:y
then values up to index y-1 are printed.
Now, if it is required to access first ‘n’ elements of the list, then there is no need to
provide index ‘0’, only the value of ‘n’ is required. Here is an example :
>>> myList[:4]
Similarly, if last ‘n’ elements are required to be accessed then only starting index is
required. Here is an example :
>>> myList[4:]
So we see that all the elements beginning from index 4 till end of the list were
displayed.
If neither starting index, nor the ending index is provided then complete list is
displayed. Here is an example :
>>> myList[:]
['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']
Lists can easily be searched for values using the index method that expects a value
that is to be searched. The output is the index at which the value is kept.
Here is an example :
>>> myList.index("revolves")
>>> myList.index("a")
True
So we see that ‘True’ was returned as ‘sun’ was present in the list.
Python provides the remove method through which we can delete elements from
a list. It expects the value which is required to be deleted.
>>> myList
['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']
>>> myList.remove("Yes")
>>> myList
['The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']
>>> myList.remove("statement")
>>> myList
['The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'for', 'sure']
Here we see that remove can be used to easily delete elements in list.
>>> myList
So we see that the sub list was deleted from the original list.
If it is required to access the last element and then to delete it, this can be done
through pop method.
>>> myList.pop()
'sure'
>>> myList
Python allows to use mathematical operators like +, * etc to be used with lists.
Here are some examples :
>>> myList
>>> myList
>>> myList
>>> myList *= 2
>>> myList
['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.', 'The', 'earth', 'revolves',
'around', 'sun', 'for', 'sure', '.']
So we see that through + operator elements could be added to the list and
through * operator we can add the complete list repeatedly towards the end.