0% found this document useful (0 votes)
27 views18 pages

Python List

The document discusses lists in Python. It defines that a list is a collection of elements that is ordered and changeable. Lists allow duplicate values and contain items separated by commas within square brackets. The values in a list can be accessed using indexes starting from 0. Some common list methods are described such as append(), extend(), len(), index(), insert(), remove(), pop(), sort(), etc. Examples are given to demonstrate creating and manipulating lists using these methods.

Uploaded by

llolalt94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
27 views18 pages

Python List

The document discusses lists in Python. It defines that a list is a collection of elements that is ordered and changeable. Lists allow duplicate values and contain items separated by commas within square brackets. The values in a list can be accessed using indexes starting from 0. Some common list methods are described such as append(), extend(), len(), index(), insert(), remove(), pop(), sort(), etc. Examples are given to demonstrate creating and manipulating lists using these methods.

Uploaded by

llolalt94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

CLASS 10

ADVANCE PYTHON
LIST
LIST

When we want to assign multiple values to a variable, we use list.


Python knows a number of compound data types, used to group together
other values. The most versatile is the list, which can be written as a list of
comma-separated values (items) between square brackets.
LIST

• List is a collection of elements which is ordered and changeable.


• Allows duplicate values.
• A list contains items separated by commas and enclosed within square
brackets ([ ]).
• All items belonging to a list can be of different data type.
• The values stored in a list can be accessed using the slice operator ([ ] and
[:]) with indexes starting at 0 in the beginning of the list.
• CREATING A LIST

To create a list, enclose the elements of the list within square brackets
and separate the elements by commas.
Syntax:
list-name= [item-1, item-2, …….., item-n]
Example:
mylist=["apple","banana","cherry"] #a list with three items
• Accessing lists:
• The values stored in a list can be accessed using the slice operator ([ ] and [:]) with
indexes.
• List-name[start:end] will give you elements between indices start to end-1.
• The first item in the list has the index zero (0).
• Examples:
>>> mylist=[25,12,36,95,14]
>>> mylist[3]
95
>>> mylist [-2]
95
>>> mylist [-5]
25
>>> mylist[-10] #IndexError: list index out of range
LIST METHODS
FUNCTION NAME DESCRIPTION
append( ) The append() method in python adds a single item to the existing list. It doesn't return a new list of
items but will modify the original list by adding the item to the end of the list

extend( ) We can add several items to the list using extend.

len( ) Find the length of the list.

index( ) Returns the index of the first occurrence of the element with the specified value. If not found it
raises a value error.

insert( ) Adds an element at the specified position.

reverse() The reverse() method reverses the elements of the list

clear() The clear() method removes all items from the list
remove( ) To remove a single element from the list by passing the value itself.

pop( ) Removes the element at the specified position using index number and returns the deleted
element.

sort( ) Sorts the list. By default in ascending order

sort(reverse=True) sorts the list in the descending order

max() max returns the elements from the list with maximum value

min() min() returns the elements from the list with minimum value

To remove multiple values from list using range ( del l1 [2:] )


Del
List: Slicing method

mylist=[34,53,14,64,37,25,18]
print(mylist[3])
print(mylist [-2])
print(mylist [-5])
print(mylist[2:6])
Example: List Functions
l1.sort(reverse=True) #sort(reverse=True) descending order
l1=[122,33,144,55,66,77] print(l1)
print(l1) l1.insert(2,500) #insert()

print(len(l1)) #len()
print(l1)
l1.remove(55) #remove()
#l1.reverse() #Reverse()
print("Removed 55 from the list",l1)
#print(l1) print(l1.pop()) #pop()
#l1.clear() #clear() l2=[100,200,300]

#print(l1) l1.append(l2) #append()


print(l1)
print(l1.index(25)) #index()
l1.extend(l2) #extend()
print(max(l1)) #max()
print(l1)
print(min(l1)) #min() print(len(l1))

l1.sort() #sort() ascending order print(l1[5])

print(l1)
#WAP to display list elements with index number in different line.

l1=[23,45,56,78,99,100]
for i in l1: #method1
print("The element is ",i,"and its index number is:",l1.index(i))

for i in range(len(l1)): #method2


print(l1[i],i)
#WAP find the sum and average of list l1.

l1=[23,45,56,78,99,100]
s=0
for i in l1:
s=s+i
print("Sum=",s)
print("Average=",s/len(l1))
'''Write a Python Program to create a list L1 with any seven numbers, and
then add 10 to all the odd values and 5 to all the even values of the list L1.
Also display the updated values in a list'''
l1=[22,14,33,55,16,17,10]
print(l1)
for i in range(len(l1)):
if(l1[i]%2==0):
l1[i]=l1[i]+5 # adding 5 to even numbers
else:
l1[i]=l1[i]+10 # adding 10 to odd numbers
print("Update list is:",l1)
Q. Write a Menu Driven program to perform the following operations on a list. Menu. 1. Insert an element into a list at a particular position 2. Remove an element from
the list based on the user's choice. 3. Print the largest element and smallest element in the list
list1=[23,45,67,89,37]
print("1. Insert an element into a list at a particular position 2. Remove an element from the list based on the user's choice. 3. Print the largest element and
smallest element in the list")
ch=int(input("Enter the choice:"))
if(ch==1):
el=int(input("Enter the number to insert:"))
p=int(input("Enter the index number to insert:"))
list1.insert(p,el)
print(list1)
elif(ch==2):
re=int(input("Enter the element to remove:"))
if re in list1:
list1.remove(re)
else:
print("Element not found")
print(list1)
elif(ch==3):
print("Largest value:",max(list1))
print("Smallest value:",min(list1))
else:
print("Invalid choice")
Q. WAP to create a list with 6 values and search for a value from the list.

ls=[23,45,67,89,10,78]
le=len(ls)
s=int(input("Enter the number to search:"))
if s in ls:
print(s,"is found","in the index number",ls.index(s))
else:
print(s,"is not found")
ASSIGNMENT QUESTIONS

Q1. WAP to calculate average height of a class of 10 students in a list.


height = [140,154,181,173,176,169,153,163,140,158]
Q2. WAP to create a list and find the minimum value from a list along with its index number.
Q3. WAP to create a list and search for an element in a given list of numbers. Print the message if the element is not in the list.
Q4. Start with the list[8,9,10,11,12]. Do the following using list functions
(a) Set the second entry (index 1) to 17
(b) Add 4, 5 and 6 to the end of the list.
(c) Remove the first entry from the list.
(d) Sort the list.
(e) Double the list. (name of the list*2)
(f) Insert 25 at index 3
Q5. Program to print elements of a list[‘p’,’y’,’t’,’h’,’o’,’n’] in separate line along with elements both
index(positive and negative.
Q6. Q1. Create two list values
L1=[2,4,6,8]
L2=[10,12,14]

Do the following functions in the list


1. Concatenate two lists L1and L2
2. Turn every item of a L1 into its square(eg:4,16,36,64)
3. Remove value12 from the list L2.
4. Return the deleted value 14 from the list L2.
5. Add a sublist L3=[30,40] to the L1.
6. Replicate the List L2 4 times.
7. Print all the list items of L1 using while loop in different line.
Q7. WAP to Create a list

num=[101,110,123,244,256,290]

Do the following operations


1. Add two elements at the end of the list(store it as an individual items).
2. Delete an element from the list (user can enter any number)
3. Return index number of a particular element (user can enter any number)
4. Access the value from 1st index number to 4th index numbers using slicing method.
5. Add an element (350) at the 3rd index number.
6. Use the built in function to remove an element and display the deleted element.
7. Sort the values in the list in descending order.
8. Find the maximum and minimum value from the given list
9. Add another list n2=[22,33,44] as the sub list in the existing list num.
10.Remove all the values from the list
THANK YOU

You might also like