Friday 4 December 2015

Python lists

List is group of values in between square brackets separated by commas.
>>> primes=[2, 3, 5, 7, 11, 13, 17, 19, 23]
>>> primes
[2, 3, 5, 7, 11, 13, 17, 19, 23]


‘primes’ is a list that contain prime numbers.
>>> students=["Hari", "Krishna", "Kiran", "Ravi"]
>>> students
['Hari', 'Krishna', 'Kiran', 'Ravi']


‘students’ is a list that contain all student names.

List can contain any type of data.
>>> objects=[1, 3, "Hello", 10.23]
>>> objects
[1, 3, 'Hello', 10.23]


You can access elements of list by using index.
>>> objects
[1, 3, 'Hello', 10.23]
>>> objects[0]
1
>>> objects[2]
'Hello'


objects[0] return the first element of list objects.
objects[1] return the second element of list objects.

-ve indexes also used to access elements of a list.
>>> objects
[1, 3, 'Hello', 10.23]
>>> objects[-1]
10.23
>>> objects[-3]
3


Slicing
Slicing is used to get sub list.

Example
Description
list[start:end]
Returns sub list from index start (included) to end index (excluded).
list[:end]
Returns sub list from index 0(included) to end index (excluded).
list[start:]
Return sub list from index start to till end.
list[-2:]
Return list from 2nd last to end.

>>> objects
[1, 3, 'Hello', 10.23]
>>> objects[-1]
10.23
>>> objects[-3]
3
>>> objects
[1, 3, 'Hello', 10.23]
>>> 
>>> objects[:]
[1, 3, 'Hello', 10.23]
>>> 
>>> objects[2:]
['Hello', 10.23]
>>> 
>>> objects[:3]
[1, 3, 'Hello']
>>> 
>>> objects[-2:]
['Hello', 10.23]


Concatenate two lists
'+' Operator is used to concatenate two lists.
>>> even=[2, 4, 6, 8, 10]
>>> odd=[1, 3, 5, 7, 9]
>>> numbers = even+odd
>>> numbers
[2, 4, 6, 8, 10, 1, 3, 5, 7, 9]

Lists are mutable; you can change the values of list.    
>>> numbers
[2, 4, 6, 8, 10, 1, 3, 5, 7, 9]
>>> numbers[0]=12
>>> numbers[1]=14
>>> numbers
[12, 14, 6, 8, 10, 1, 3, 5, 7, 9]


Add elements to end of list
List provides ‘append’ method to add new elements to the end of a list.    
>>> numbers
[12, 14, 6, 8, 10, 1, 3, 5, 7, 9]
>>> 
>>> numbers.append(11)
>>> numbers.append(13)
>>> 
>>> numbers
[12, 14, 6, 8, 10, 1, 3, 5, 7, 9, 11, 13]


Assignment to slices
If you want to replace sequence of elements in a list, you can use slice notation.

numbers[2:5] = [21, 22, 23]
Above statement replace elements at index 2, 3, 4 with 21, 22, 23 respectively.

numbers[:] = []
Above statement clear the list by replacing all the elements with an empty list. 
>>> numbers
[12, 14, 6, 8, 10, 1, 3, 5, 7, 9, 11, 13]
>>> 
>>> numbers[2:5]=[21, 22, 23]
>>> numbers
[12, 14, 21, 22, 23, 1, 3, 5, 7, 9, 11, 13]
>>> 
>>> numbers[:] = []
>>> numbers
[]


Get length of the list
By using ‘len’ function, you can get the length of the list.
>>> vowels=['a', 'e', 'i', 'o', 'u']
>>> len(vowels)
5


Nested lists
A list can be nested in other list. For example, in below example, numbers contains 3 lists, first list represent odd numbers, second list represent even numbers and third list represent prime numbers.
>>> numbers=[[1, 3, 5, 7],[2, 4, 6, 8],[2, 3, 5, 7, 11]]
>>> numbers
[[1, 3, 5, 7], [2, 4, 6, 8], [2, 3, 5, 7, 11]]
>>> 
>>> numbers[0]
[1, 3, 5, 7]
>>> 
>>> numbers[1]
[2, 4, 6, 8]
>>> 
>>> numbers[2]
[2, 3, 5, 7, 11]
>>>
>>>
>>> len(numbers)
3
>>> len(numbers[0])
4
>>> len(numbers[1])
4
>>> len(numbers[2])
5
>>> 
>>> numbers[0][1]=9
>>> numbers[1][1:4] = [10, 12, 14]
>>> numbers
[[1, 9, 5, 7], [2, 10, 12, 14], [2, 3, 5, 7, 11]]





  





Previous                                                 Next                                                 Home

No comments:

Post a Comment