LISTS
INTRODUCTION
Syntax is :
Variable name [index]
Index - integer value (+ve / -ve)
Positive value - counting forward from beginning of the list
Negative value - counting backward from end of the list.
INTRODUCTION
A list containing another list known as nested list.
LISTS EXAMPLE
Use double subscript to fetch the sub list in a given list.
LISTS EXAMPLE
Similarly, L2[2][0] will fetch you 4 value
L2[2][2] will fetch you 7 value.
STATE DIAGRAM OF LIST
>>> L1 = [45,56,67,20]
State Diagram would be:
Forward Indexing
Backward Indexing
0
1
2
3
-4
-3
-2
-1
45
56
67
20
LIST INDEX
LIST elements
LIST - Example
CREATING MULTIPLE LISTS – LIST MAPPING
Creating Multiple List using assignment statement
Creating a list and mapping to other lists.
LIST CONCATENATION
A and B are two different LISTS and third LIST is created by combining both A and B.
Deleting Elements of LIST
It is possible to delete/remove element(s) from the list. There are many ways of doing so:
(i) If index is known, we can use pop ( ) or del
(ii) If the element is known, not the index, remove ( ) can be used.
(iii) To remove more than one element, del ( ) with list slice can be used.
(iv) Using assignment operator
Deleting Elements of LIST
1. pop ( ) Method
2. del Method
3. remove ( ) Method
Deleting Elements of LIST
1. pop ( ) Method
It removes the element from the specified index, and also return the element which was removed.
Its syntax is:
List.pop ([index])
Deleting Elements of LIST
1. pop ( ) Method
For Example:
Popped element 67 is assigned to x
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]
del method example
Deleting Elements of LIST
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)
contd…
Deleting Elements of LIST
3. remove ( ) Method
SOME MORE METHODS
SOME MORE METHODS
1. insert () Method
2. reverse ( ) Method
3. sort ( ) Method
4. clear ( ) Method
5. index ( ) Method
6. extend ( ) Method
OTHER METHODS
1. insert () Method
This 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)
OTHER METHODS
1. insert () Method
For Example:
OTHER METHODS
2. reverse ( ) Method
This 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.
OTHER METHODS
2. reverse ( ) Method
For Example:
OTHER METHODS
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.
OTHER METHODS
3. sort ( ) Method
For Example:
Default Ascending
Order sorting
OTHER METHODS
3. sort ( ) Method
For Example:
Descending order
OTHER METHODS
3. sort ( ) Method
For Example:
Sorting using key value (length of string)
OTHER METHODS
4. clear ( ) Method
clear() method deletes
or clears the content of list. For Example:
OTHER METHODS
5. index ( ) Method
To know the index of an
element in a given list
For Example:
ABOVE AVERAGE PROGRAMS ON LISTS
8. Write a Python program to print the numbers of a specified list after removing even numbers from it.
def num_prn():
num = [7,8, 120, 25, 44, 20, 27]
num = [x for x in num if x%2!=0]
print(num)
Tuples in Python
•Tuples are very similar to lists, but they are immutable (i.e., unchangeable)
•Tuples are written with round brackets as follows:
Tuples in Python
•Like lists, tuples can:
•Contain any and different types of elements
•Contain duplicate elements (e.g., (1, 1, 2))
•Be indexed exactly in the same way (i.e., using the [] brackets)
•Be sliced exactly in the same way (i.e., using the [::] notation)
•Be concatenated (e.g., t = (1, 2, 3) + (“a”, “b”, “c”))
•Be repeated (e.g., t = (“a”, “b”) * 10)
•Be nested (e.g., t = ((1, 2), (3, 4), ((“a”, “b”, ”c”), 3.4))
•Be passed to a function, but will result in pass-by-value and not pass-by-reference outcome since it is immutable
•Be iterated over