#Sample thislist = ["apple", "banana", "cherry"]print("MyList : ",thislist)thislist = ["apple", "banana", "cherry", "apple", "cherry"]print(thislist) #Length of the listthislist = ["apple", "banana", "cherry"]print(len(thislist))#Access List Itemsthislist = ["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 Existsthislist = ["apple", "banana", "cherry"]if "apple" in thislist: print("Yes, 'apple' is in the fruits list") #Change Item Valuethislist = ["apple", "banana", "cherry"]thislist[1] = "blackcurrant"print(thislist)#Append Items#To add an item to the end of the list, use the append() methodthislist = ["apple", "banana", "cherry"]thislist.append("orange")print(thislist)#Insert Itemsthislist = ["apple", "banana", "cherry"]thislist.insert(1, "orange")print(thislist)#Extend List#To append elements from another list to the current list, use the extend() methodthislist = ["apple", "banana", "cherry"]tropical = ["mango", "pineapple", "papaya"]thislist.extend(tropical)print(thislist)#Remove List Itemsthislist = ["apple", "banana", "cherry"]thislist.remove("banana")print(thislist)#Remove Specified Index#The pop() method removes the specified indexthislist = ["apple", "banana", "cherry"]thislist.pop(1)print(thislist)#Remove the last itemthislist = ["apple", "banana", "cherry"]thislist.pop()print(thislist)#Delete the entire listthislist = ["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 loopthislist = ["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 Loopthislist = ["apple", "banana", "cherry"]i = 0while i < len(thislist): print(thislist[i]) i = i + 1#List Comprehensionfruits = ["apple", "banana", "cherry", "kiwi", "mango"]newlist = []for x in fruits: if "a" in x: newlist.append(x)print(newlist) #Sort Listthislist = ["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 Functiondef myfunc(n): return abs(n - 50)thislist = [100, 50, 65, 82, 23]thislist.sort(key = myfunc)print(thislist)#Copy Listthislist = ["apple", "banana", "cherry"]mylist = thislist.copy()print(mylist)#Another Waythislist = ["apple", "banana", "cherry"]mylist = list(thislist)print(mylist)#List Countfruits = ['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)
No comments:
Post a Comment