Lists are used to store multiple items in a single variable.
Example :
Students=['Yash','Vidya','Gauri']
print(Students)Students=['Yash','Vidya','Gauri','Prachi']
print(Students[1])Students=['Yash','Vidya','Gauri','Prachi']
print(Students[0:3])Students=['Yash','Vidya','Gauri','Prachi']
print(Students[-1])- Ordered
Lists are ordered, it means that the items have a defined order, and that order will not change.
- Allow Duplicates
Lists can have items with the same value
Fruits = ["apple", "banana", "cherry", "apple", "cherry"]
print(Fruits)- List can contain Different Data Type
A list can contain different data types
RandomData = ["abc", 34, True, 40, "Hi"]
print(RandomData)- Changeable
The list is changeable, that we can change, add, and remove items in a list after it has been created.
- Update =
Courses=['c','c++','Python','Java']
print("Before Changes",Courses)
Courses[1]="C++ Programming"
print("After Changes",Courses)- Insert =
Courses=['c','c++','Python','Java']
print("Before Changes:",Courses)
Courses.insert(1,'Android Development')
print("After Changes:",Courses)- Append =
The append() method appends an element to the end of the list.
Courses=['c','c++','Python','Java']
print("Before Changes:",Courses)
Courses.append('Android Development')
print("After Changes:",Courses)- Remove =
The remove() method removes the specified item.
Fruits = ["apple","banana","orange","grepes"]
Fruits.remove("banana")
print(Fruits)- Pop =
The pop() method takes a single argument (index).
Fruits = ["apple","banana","orange","grepes"]
Fruits.pop(2)
print(Fruits)- Delete =
Fruits = ["apple","banana","orange","grepes"]
del Fruits[1]
print(Fruits)- Clear =
The clear() method removes all the elements from a list.
Fruits = ["apple","banana","orange","grepes"]
Fruits.clear()
print(Fruits)- Sort =
List objects have a sort() method that will sort the list alphanumerically, ascending,descending.
Ascending Order=
alphabets=['A','R','V','S','P']
alphabets.sort()
print(alphabets)Descending Order=
alphabets=['A','R','V','S','P']
alphabets.sort(reverse=True)
print(alphabets)1️⃣Write a program to implement deletion of items from list using four different methods.
👁 Show Answer
colour=["Red","Pink","Orange","Yellow","Green"]
colour.remove("Pink")
print(colour) #Remove Method
colour=["Red","Pink","Orange","Yellow","Green"]
colour.pop(3)
print(colour) #Pop Method
colour=["Red","Pink","Orange","Yellow","Green"]
del colour[1]
print(colour) #Delete Method
colour=["Red","Pink","Orange","Yellow","Green"]
colour.clear()
print(colour) #Clear Method2️⃣ Write a program to implement sort the given list in ascending and descending order.
👁 Show Answer
colour=["Red","Pink","Orange","Yellow","Green"]
colour.sort()
print(colour) #Ascending order
colour=["Red","Pink","Orange","Yellow","Green"]
colour.sort(reverse=True)
print(colour) #descending order
