# Create a list
new_list =[]# Add items to the list
item1 ="string1"
item2 ="string2"
new_list.append(item1)
new_list.append(item2)# Access list itemsprint(new_list[0])print(new_list[1])
my_list =['p','r','o','b','e']# first itemprint(my_list[0])# p# third itemprint(my_list[2])# o# fifth itemprint(my_list[4])# e# Nested List
n_list =["Happy",[2,0,1,5]]# Nested indexingprint(n_list[0][1])print(n_list[1][3])# Error! Only integer can be used for indexingprint(my_list[4.0])
list=[1,2,3,4,5,6]print(list)# It will assign value to the value to second index list[2]=10print(list)# Adding multiple element list[1:3]=[89,78]print(list)# It will add value at the end of the list list[-1]=25print(list)
list=[0,1,2,3,4]print("printing original list: ");for i inlist:print(i,end=" ")list.remove(2)print("
printing the list after the removal of first element...")for i inlist:print(i,end=" ")
list1 =[10,20,4,45,99]
mx=max(list1[0],list1[1])
secondmax=min(list1[0],list1[1])
n =len(list1)for i inrange(2,n):if list1[i]>mx:
secondmax=mx
mx=list1[i]elif list1[i]>secondmax and
mx != list1[i]:
secondmax=list1[i]print("Second highest number is : ",str(secondmax))
Output:-
Second highest number is:45
# A list is a collection of items. # Lists are mutable: you can change their elements and their size.# Similar to List<T> in C#, ArrayList<T> in Java, and array in JavaScript.
foo =[1,2,True,"mixing types is fine"]print(foo[0])# Output - 1
foo[0]=3print(foo[0])# Output - 3
# list of numbers
n_list =[1,2,3,4]# 1. adding item at the desired location# adding element 100 at the fourth location
n_list.insert(3,100)# list: [1, 2, 3, 100, 4]print(n_list)# 2. adding element at the end of the list
n_list.append(99)# list: [1, 2, 3, 100, 4, 99]print(n_list)# 3. adding several elements at the end of list# the following statement can also be written like this:# n_list + [11, 22]
n_list.extend([11,22])# list: [1, 2, 3, 100, 4, 99, 11, 22]print(n_list)
# Creating a List
grocery_list =["apple","watermelon","chocolate"]# Appending items to a list
grocery_list.append("milk")# Changing an item on the list
grocery_list[1]="apple juice"# Deleting an item on the list
grocery_list.remove("watermelon")# Sort list in alphabetical order
grocery_list.sort()# Joining lists
utensils =["fork","spoon","steak knife"]list= grocery_list + utensils
# Printing the listsprint(*list, sep=", ")