Computer Science
Differentiate between append() and extend() methods and provide examples of each.
Python List Manipulation
1 Like
Answer
| append() method | extend() method |
|---|---|
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 is list.append(item). | The syntax is list.extend(list1). |
| For example, list1 = [1, 2, 3] list1.append(4) print(list1) | For example, list1 = [1, 2, 3] list1.extend([4, 5]) print(list1) |
Answered By
2 Likes
Related Questions
Observe the code given below and answer the following questions:
n = ............... #Statement 1 if ............... : #Statement 2 print ("Please enter a positive number") elif ............... : #Statement 3 print ("You have entered 0") else: ............... #Statement 4(a) Write the input statement to accept n number of terms — Statement 1.
(b) Write if condition to check whether the input is a positive number or not — Statement 2.
(c) Write if condition to check whether the input is 0 or not — Statement 3.
(d) Complete Statement 4.
Explain the membership operators in String.
Consider the string
mySubject:mySubject = "Computer Science"What will be the output of:
print(mySubject[:3])print(mySubject[-5:-1])print(mySubject[::-1])print(mySubject*2)
Consider the dictionary
stateCapital:stateCapital = {"AndhraPradesh" : "Hyderabad", "Bihar" : "Patna", "Maharashtra" : "Mumbai", "Rajasthan" : "Jaipur"}Find the output of the following statements:
print(stateCapital.get("Bihar"))print(stateCapital.keys())print(stateCapital.values())print(stateCapital.items())