Python List
Python List
ADVANCE PYTHON
LIST
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
index( ) Returns the index of the first occurrence of the element with the specified value. If not found it
raises a value error.
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.
max() max returns the elements from the list with maximum value
min() min() returns the elements from the list with minimum value
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)
#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))
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
num=[101,110,123,244,256,290]