Lists
Lists
3rd Element of
list modified.
LISTS EXAMPLE
List containing string type data
Forward Indexing 0 45 -4
1 56 -3
2 67 -2
3 20 -1 Backward Indexing
LIST INDEX
LIST elements
STATE DIAGRAM OF LIST
>>> L2 = [“Mouse”,Pendrive”,”RAM”]
State Diagram would be:
Forward Indexing
0 Mouse -3
1 Pendrive -2
2 RAM -1
Backward Indexing
LIST INDEX
LIST elements
STATE DIAGRAM OF LIST
>>> L3= []
State Diagram would be:
WORKING WITH LIST INDEX
WORKING WITH LIST INDEX
Creating a list
and mapping to
other lists.
Elements
read during
run time
OUTPUT
TRAVERSING LIST
TRAVERSING LIST
OUT PUT
Deleting Elements of LIST
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
List.pop ([index])
Deleting Elements of LIST
1. pop ( ) Method
For Example:
2. del Method
3. remove ( ) Method
>>>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
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:
def sum_list(items):
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers
print(sum_list([1,2,-8]))
PROGRAMS ON LISTS
BELLOW AVERAGE PROGRAMS ON LISTS
def check_list():
l = []
if not l:
print("List is empty")
PROGRAMS ON LISTS
BELLOW AVERAGE PROGRAMS ON LISTS
def clone_list():
original_list = [10, 22, 44, 23, 4]
new_list = list(original_list)
print(original_list)
print(new_list)
PROGRAMS ON LISTS
def printValues():
l = list()
for i in range(1,21):
l.append(i**2)
print(l[:5])
print(l[-5:])
printValues()
ABOVE AVERAGE PROGRAMS ON LISTS
ABOVE AVERAGE PROGRAMS ON LISTS
7 Write a Python program to generate and print a
list except for the first 5 elements, where the
values are square of numbers between 1 and 30
(both included).
def printValues():
l = list()
for i in range(1,21):
l.append(i**2)
print(l[5:])
printValues()
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)
ABOVE AVERAGE PROGRAMS ON LISTS
9. Write a Python program to print a specified list
after removing the 0th, 4th and 5th elements.
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink',
'Yellow']
Expected Output : ['Green', 'White', 'Black']
def prn_list()
color = ['Red', 'Green', 'White', 'Black', 'Pink',
'Yellow']
color = [x for (i,x) in enumerate(color) if i not in
(0,4,5)]
print(color)
CLASS WORK/HOME WORK
CLASS WORK/HOME WORK
1. Write a Python program to generate a 3*4*6
3D array whose each element is *.
2.Write a Python program to shuffle and print a
specified list.
3. Write a Python program to get the difference
between the two lists.
4. Write a Python program access the index of a
list.
5. Write a Python program to convert a list of
characters into a string.
6. Write a Python program to find the index of an
item in a specified list
Class Test
Class Test
1. What is list? 02
2. Explain the various ways of creating
list 03
3. Explain 5 list built in methods
10
4. Explain sort method with its various
forms 05
Thank You