- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Lists / of Python List In-Built Functions
# How to append data to a list? fruits = ["orange", "mangoes", "banana", "apples"] # print(fruits) ranks = [1, 2, 3, 4, 5] fruits.append(ranks) # fruits.append("green") print(fruits) #How to Add Elements of a List to Another List? fruits = ["orange", "mangoes", "banana", "apples"] print(id(fruits)) ranks = [1, 2, 3, 4, 5] fruits.extend(ranks) print(id(fruits)) print(fruits) # What is the difference b/w extend and + in list? fruits = ["orange", "mangoes", "banana", "apples"] print(id(fruits)) ranks = [1, 2, 3, 4, 5] print(fruits + ranks) print(id(fruits + ranks)) # How to Inserts Element to The List? fruits = ["orange", "mangoes", "banana", "apples"] fruits.insert(1,"grapes") print(fruits) # How to Removes Element from the List? fruits = ["orange", "mangoes", "banana", "apples"] fruits.remove("orange") print(fruits) # What is the difference b/w del and remove? fruits = ["orange", "mangoes", "banana", "apples"] del fruits[1] print(fruits.remove("orange")) print(fruits) # How to get index value based on index in lists? fruits = ["orange", "mangoes", "banana", "apples"] print(fruits.index("banaa")) # How to count element occurrences in a list? fruits = ["orange", "mangoes", "banana", "apples","mangoes"] print(fruits.count("mangoes")) # How to Removes Element at Given Index? fruits = ["orange", "banana", "apples","mangoes"] print(fruits.pop()) print(fruits) print(fruits) print(fruits.pop(2)) print(fruits) print(fruits.pop()) print(fruits.pop()) print(fruits) for fruit in range(4): a = fruits.pop() print(a) print(fruits) print(fruits.pop(2)) # How to Reverses a List? fruits = ["orange", "mangoes", "banana", "apples","mangoes"] print(fruits) fruits = fruits[::-1] # fruits.reverse() print(fruits) # How to sorts elements of a list? fruits = ["orange", "mangoes", "banana", "apples","mangoes"] fruits.sort(key=None, reverse=True) print(fruits) fruits.sort(key=None, reverse=True) print(fruits) # How to Removes all Items from the List? fruits = ["orange", "mangoes", "banana", "apples","mangoes"] # fruits.clear() del fruits print(fruits) # How to sort list? fruits = [("oranges","11"),("apples","22"),("mangoes","33"),("banana","33")] fruits.sort(key=lambda x:x[0], reverse=False) print(fruits)