LIST MANIPULATION
List
thislist = ["apple", "banana", "cherry"]
print(thislist)
output
['apple', 'banana', 'cherry']
MUTABLE & IMMUTABLE TYPES
Immutable –int , float, str, Boolean , tuple
Mutable – List, Dictionary, set
Creating and Accessing List
Types of list
L=[] or L=list()
Long list
LL=[1,2,4 ……… ]
Nested List: A list inside a list
NL=[1,2,[‘A’,’B’],4,5]
Creating a list
Program Output
Accessing List
Operators and functions in List are
L[i] : returns the item at the index i (The first item has index 0]
L[i:j] : returns a new list , containing the objects at indexes between i and j (excluding index j)
Membership operators : Both in and not in operators works on list just like the way they work in strings
Traversing a List
for <item> in <List> :
process each item in list
Traversing a List
process each item in list
for i in range(len(L)):
print(L[i])
List operations
JOINING LISTS
list+number
list+string
list+complex-number
Replicating or Repeating List
SLICING THE LIST
PROGRAM OUTPUT
PROGRAM OUTPUT
PROGRAM OUTPUT
Program
4. Enter the list and print the even numbers
5. Enter a list of numbers and print the sum of all the odd numbers in the list
6. Enter two list of numbers and form a new list such that the odd positions should be the numbers in the first list and even positions should be the numbers in the second list.(Assume both the list are of the same length)
7. Enter a list of numbers and print largest number in the list.
8. Enter a list of numbers and print largest number in the list. (Do not use in built functions)
9. Enter a list of Strings and print the string with the smallest length in the list.
(Do not use in built functions)
10. Create a list with n terms of fibonacci series.
List functions and methods
The general syntax for list functions and method is:
<list Object>.<method name>()
used as list.index(<item>)
eg: L1=[12,13,14,15]
print(L1.index(14))
Ans: 2
L1=[13,18,11,16,18,14]
print(L1.index(18))
Ans : 1
print(L1.index(33))
Ans: Error #Value Error
2. The append method
Syntax : List.append(<Item>) # item means the value
It adds an element to the end of the list.
eg: L1=[12,’hello’,’@@’]
L1.append(“Python”)
print(L1)
Ans:
[12, 'HELLO', '@@', 'Python']
3. The extend method
Syntax is list.extend(<list>)
This takes a list as an argument instead of an item and adds to the end of the existing list
eg t1=[‘a’,’b’,’c’]
t2=[‘c’,’d’]
t1.extend(t2)
print(t1)
[‘a’,’b’,’c’,’c’,’d’]
Difference between append and extend
While append() function adds one element to a list, extend() can add multiple elements from a list passed as an argument
t1=[1,3,5]
t2=[7,8]
t1.append(10)
print(t1)
t1.append(12,14)
print(t1)
t1.extend(12)
print(t1)
t1.extend(t2)
print(t1)
t1=[1,3,5]
t1.append([12,13])
print(t1)
Ans: [1,3,5,[12,13]]
print(len(t1))
Ans: 4
t1=[1,3,5]
t1.extend([14,15])
print(t1)
Ans: [1,3,5,14,15]
print(len(t1))
Ans: 5
4. The insert method
L1=[‘a’,’e’,’o’]
L1.insert(2,’p’)
print(L1)
Ans: [‘a’,’e’,’p’,’o’]
L1.insert(-9,’k’)
print(L1)
Ans: [‘k’,‘a’,’e’,’p’,’o’]
Note: If the index is less than 0 and not equal to any of the valid negative indexes of the list then the value is prepended i.e added in the beginning of the list.
5. The pop method
L1=[10,20,30,40,50]
x=L1.pop(0)
print(x)
Ans 10
x=L1.pop()
print(x)
Ans: 50
print(L1)
Ans: [20,30,40]
6. The remove method
L1=[10,20,30,40,50]
L1.remove(20)
print(L1)
Ans: [10,30,40,50]
L1.remove(40)
print(L1)
Ans: [10,30,50]
L1.remove(80)
error as 80 not an item in the list (ValueError)
7. The clear method
8.The count method
9. The reverse method
10. The sort method
Programs
6. Program to create a list in the following pattern
[10,5,20,15,30,25….N] where N is the number of elements in the list.
7. Program to display the frequency of all the elements in the list.
9.Write a menu driven program to perform the following
1. Accept a list
2. search for an element in the list
3. Delete the element when index is given
4. Delete the element when value is given
5. Insert the element at any position
6. Display the list
7. Exit
10. Accept a List from the user, sort the list in ascending order and accept a new number and insert the number according to its proper position.
Eg : L=[45,12,3,56,90,52]
Sorted List = [3,12,45,52,56,90]
Accept a new number 60
Final list is = [3,12,45,52,56,60,90]
11. Write a menu driven program to do the following:
Two Dimensional List
Regular 2D lists are the nested lists with these properties:
Ragged List