LISTS PDF RSV
LISTS PDF RSV
An Index Error appears, if you try and access element that does
not exist in the list
IndexError: list index out of range
>>> list3 = ['A V Raman', 35, 'TGT Computer', 470.00] # a list with multiple type values
With Tuple
We can also create a list of single or single digits through keyboard input:
To store list through input in Python we can use eval() to convert the list items.:
Accessing/Printing List Elements
We can access individual list elements using
indexing and a range of elements using slicing.
A list index starts from 0.
Python indexes the list element from left to right
and from right end to left.
From left to right, the first element of a list has the
index 0 and from right end to left, the extreme right
index element of a list is –1.
Individual
For example:
elements
>>> print (list1) # prints in82,a98,list
all elements [67, can
92, 78, 87] be accessed by
specifying the list name followed by a number in
>>> print (list1[4]) # prints 5th element 78
square brackets ( [ ] ).
>>> list1[-2] # prints 2nd last element 78
Example of list:
Slicing List Elements
Selecting a slice is similar to selecting an element(s) of a list.
Subsets of lists can be taken using the slice operator with two indices in square brackets
separated by a colon ( [m:n] ).
The operator [m:n] returns the part of the list from the mth position and up to but not
including position n, i.e., n – 1.
List Slices
Examples
>>> L=[10,20,30,40,50]
>>> print(L[1:4])
[20, 30, 40] #print elements 1st index to 3rd index
>>> print(L[3:])
[40, 50] #print elements from 3rd index onwards
>>> print(L[:3])
[10, 20, 30] #print elements 0th index to 2nd index
COPYING ALTERNATIVE
ELEMENTS
OF LIST BY SLICING
Output
1234
Traversing a List
Using for loop
L=[1,2,3,4,5] L1=[1,2,3,4,5]
for i in L: for i in range (5):
print(i) print(L1[i])
Output: Output:
12345 12345
READING LIST THROUGH KEYBOARD
USE OF append ( ) Method
• append () method is used to add one element to the end
of list. Every time new element is assigned using append
it will be added at the rear side of list
• Syntax:
• Listobject.append(element)
• Example
• L1.append(5) OR
• element=int(input())
• L1.append(element)
Deleting Elements of LIST
2. del Method
del removes the specified element from the list, but does not return the deleted value.
Syntax :
del Listobject[index]
3. remove ( ) Method
In case, we know the element to be deleted not the index, of the element, then remove ()
can be used.
Syntax is:
listobject.remove(value)
>>>L1.remove(60)
Examples
>>> l=[10,20,30,40,50]
>>> l.pop() #last index elements popped
50
>>> l.pop(1) # using list index
20
>>> l.remove(10) #using element
>>> print(l)
[30, 40]
>>> l=[1,2,3,4,5,6,7,8]
>>> del l[2:5] #using range
>>> print(l)
[1, 2, 6, 7, 8]
1. insert () Method
insert () method allows us to insert an element, at the given position specified
by its index, and the remaining elements are shifted to accommodate the new
element.
insert () requires two arguments-index value and item value.
Its syntax is
list. insert (index, item)
2. reverse ( ) Method
reverse() method can be used to reverse the elements of the list in place
Its syntax is:
list.reverse ( )
Method does not return anything as the reversed list is stored in the same variable
3. sort ( ) Method
For arranging elements in an order Python provides a method sort ( ) and a function
sorted ( ).
sort ( ) modifies the list in place and sorted ( ) returns a new sorted list.
Default Ascending
Order sorting
Descending order
5. index ( ) Method
index() method is used to know the index of an element in a given list
6. extend ( ) Method
extend() method is used for Extending a given list with another list.
Difference between append() and extend()
• append() allows to add only 1 item to a list, extend() can add multiple items to a list
Linear search program