#Sample
thislist = ["apple", "banana", "cherry"]
print("MyList : ",thislist)
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
#Length of the list
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
#Access List Items
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])
#Check if Item Exists
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
#Change Item Value
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
#Append Items
#To add an item to the end of the list, use the append() method
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
#Insert Items
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
#Extend List
#To append elements from another list to the current list, use the extend() method
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
#Remove List Items
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
#Remove Specified Index
#The pop() method removes the specified index
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
#Remove the last item
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
#Delete the entire list
thislist = ["apple", "banana", "cherry"]
del thislist
#Clear the List
#The clear() method empties the list.
#The list still remains, but it has no content.
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
#Loop Through a List
#You can loop through the list items by using a for loop
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
#Loop Through the Index Numbers
#You can also loop through the list items by referring to their index number.
#Use the range() and len() functions to create a suitable iterable.
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
#Using a While Loop
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i = i + 1
#List Comprehension
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
#Sort List
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
print(thislist)
#Customize Sort Function
def myfunc(n):
return abs(n - 50)
thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)
print(thislist)
#Copy List
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
#Another Way
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
#List Count
fruits = ['apple', 'banana', 'cherry']
x = fruits.count("cherry")
print(x)
points = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = points.count(9)
print(x)