Computer Science
Differentiate between append() and extend() methods of list.
Python List Manipulation
1 Like
Answer
append() | extend() |
---|---|
The append() method adds a single item to the end of the list. The single element can be list, number, strings, dictionary etc. | The extend() method adds one list at the end of another list. |
The syntax of append() method is list.append(item) . | The syntax of extend() method is list.extend(list1) . |
For example: lst1 = [10, 12, 14] lst1.append(16) print(lst1) Output — [10, 12, 14, 16] | For example: t1 = ['a', 'b', 'c'] t2 = ['d', 'e'] t1.extend(t2) print(t1) Output — ['a', 'b', 'c', 'd', 'e'] |
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.