Informatics Practices
Differentiate between append() and insert() methods of list.
Python List Manipulation
1 Like
Answer
Insert Method | Append Method |
---|---|
The insert() method is used to add an element at a specific index in the list. | The append() method is used to add an element at the end of the list. |
The syntax is: list.insert(index, element) . | The Syntax is: list.append(element) . |
Example: my_list.insert(2, 'hello') will insert the string 'hello' at index 2 in the list my_list. | Example: my_list.append('world') will add the string 'world' at the end of the list my_list. |
Answered By
2 Likes
Related Questions
What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[::2] print(myList)
Write the output of the following Python program code:
Str2 = list("Cam@123*") for i in range(len(Str2)-1): if i==5: Str2[i] = Str2[i]*2 elif (Str2 [i].isupper()): Str2 [i] = Str2 [i]*2 elif (Str2 [i].isdigit()): Str2 [i] = 'D' print(Str2)
Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.
Write a program to find the largest and the second largest elements in a given list of elements.