List

1. Python lists are the data structure that is capable of holding different type of data.

2. Python lists are mutable i.e., Python will not create a new list if we modify an element in the list.

3. It is a container that holds other objects in a given order. Different operation like insertion and deletion can be performed on lists.

4. A list can be composed by storing a sequence of different type of values separated by commas.

5. A python list is enclosed between square([]) brackets.

6. The elements are stored in the index basis with starting index as 0.

eg:

data1=[1,2,3,4];
data2=['x','y','z'];
data3=[12.5,11.6];
data4=['raman','rahul'];
data5=[];
data6=['abhinav',10,56.4,'a'];

Accessing Lists

A list can be created by putting the value inside the square bracket and separated by comma.

Syntax:

<list_name>=[value1,value2,value3,...,valuen];

For accessing list :

<list_name>[index]

Different ways to access list:

Eg:

data1=[1,2,3,4];
data2=['x','y','z'];
print(data1[0])
print(data1[0:2])
print(data2[-3:-1])
print(data1[0:])
print(data2[:2])

Output:

>>>
1
[1, 2]
['x', 'y']
[1, 2, 3, 4]
['x', 'y']
>>>

Note: List do not store the elements directly at the index. In fact a reference is stored at each index which subsequently refers to the object stored somewhere in the memory. This is due to the fact that some objects may be large enough than other objects and hence they are stored at some other memory location.

List Operations:

Various Operations can be performed on List. Operations performed on List are given as:

a) Adding Lists:

Lists can be added by using the concatenation operator(+) to join two lists.

Eg:

list1=[10,20]
list2=[30,40]
list3=list1+list2
print(list3)

Output:

>>>  
[10, 20, 30, 40]
>>>

Note: '+'operator implies that both the operands passed must be list else error will be shown.

Eg:

list1=[10,20]
list1+30
print (list1)

Output:

Traceback (most recent call last):
        File "C:/Python27/lis.py", line 2, in <module>
            list1+30

b) Replicating lists:

Replicating means repeating . It can be performed by using '*' operator by a specific number of time.

Eg:

list1=[10,20]
print(list1*1)

Output:

>>>  
[10, 20]
>>>

c) List slicing:

A subpart of a list can be retrieved on the basis of index. This subpart is known as list slice.

Eg:

list1=[1,2,4,5,7]
print(list1[0:2])
print(list1[4])
list1[1]=9
print(list1)

Output:

>>>  
[1, 2]
7
[1, 9, 4, 5, 7]
>>>

Note: If the index provided in the list slice is outside the list, then it raises an IndexError exception.

Other Operations:

Apart from above operations various other functions can also be performed on List such as Updating, Appending and Deleting elements from a List:

a) Updating elements in a List:

To update or change the value of particular index of a list, assign the value to that particular index of the List.

Syntax:

<list_name>[index]=<value>

Eg:

data1=[5,10,15,20,25]
print("Values of list are: ")
print(data1)
data1[2]="Multiple of 5"
print("Values of list are: ")
print(data1)

Output:

>>>  
Values of list are:  
[5, 10, 15, 20, 25]
Values of list are:  
[5, 10, 'Multiple of 5', 20, 25]
>>>

b) Appending elements to a List:

append() method is used to append i.e., add an element at the end of the existing elements.

Syntax:

<list_name>.append(item)

Eg:

list1=[10,"rahul",'z']
print "Elements of List are: "
print list1
list1.append(10.45)
print "List after appending: "
print list1

Output:

>>>  
Elements of List are:  
[10, 'rahul', 'z']
List after appending:  
[10, 'rahul', 'z', 10.45]
>>>

c) Deleting Elements from a List:

del statement can be used to delete an element from the list. It can also be used to delete all items from startIndex to endIndex.

Eg:

list1=[10,'rahul',50.8,'a',20,30]
print list1
del list1[0]
print list1
del list1[0:3]
print list1

Output:

>>>  
[10, 'rahul', 50.8, 'a', 20, 30]
['rahul', 50.8, 'a', 20, 30]
[20, 30]
>>>