Python Lists list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]
The most basic data structure is sequence that is used in the python programming. In a sequence each and every element is assigned a specific number that is called as it's position or the index. The index of the very first element of the sequence is zero and then it increases with each element.
There are six inbuilt sequence types used in python, but, list, tuples are ost commonly used. So let us study about these in this section.
There are some operations that you can do with all the types of sequences and these include indexing, adding, slicing, multiplying and checking for membership. In addition to this you can use the built in functions in python for finding the length of a sequenceand find the smallest and the largest element.
Python Lists
The list data type is a type of the sequence that is considered as the most versatile data type that is available in python. In a list the data can be added in a very simple way, the items are written by the comma separated values and these values are put in betwween the square brackets.
The most important and interesting thing about the lists is that the items thata re to be stored in a list need not to be of the same data type i.e. they can of different data types.
It is very simple to create a list we just need to put the values in betwen a square bracket and the values are separated by the commas. To better understand this let us consider the following example:
Just like we have the indices in string, we have the indices in the lists too and they also start with the index value 0. The other operations that can be performed on the lists are thet they can be sliced and concatenated and so on.
Accessing Values in Lists list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]
If we want to access the values in a list, then we need to use the square brackets with the index values for slicing to obtain the value that is avalable at that index. To understand this let us consider the following example:
When the lines of the above code are executed then it will produce the following result:
list1[0]: physics list2[1:5]: [2, 3, 4, 5]
Updating Lists list = ['physics', 'chemistry', 1997, 2000]; print "Value available at index 2 : " print list[2] list[2] = 2001; print "New value available at index 2 : " print list[2]
In a list if you want to update the elements of a list then you can update them in two ways, you can update sigle or multiple elements of the list, you just need to give the slice on the left hand side of the assignment operator and you can add the elemets in the list with the help of append() method. For better understanding let us consider the following example:
When the lines of the above code are executed then it will produce the following result:
Value available at index 2 : 1997 New value available at index 2 : 2001
Delete List Elements list1 = ['physics', 'chemistry', 1997, 2000]; print list1 del list1[2]; print "After deleting value at index 2 : " print list1
If you want to delete or remove the elements in a list then you can either of the two statements, thedel statement or the remove method(). The del statement is used when you know exactly which element(s) you are deleting. The remove() method is used when you you don't know that which element you are deleting.
When the lines of the above code are executed then it will produce the following result:
['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]
Functions and methods with their description:
Functions and methods | Description |
---|---|
cmp(list1,list2) | Compares elements of bothe lists |
len(list) | Gives total length of the list |
max(list) | Returns item from the list with max value. |
min(list) | Returns item from the list with min value. |
list(seq) | Converts a tuple into list |
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |