27. Suppose L=["abc", [6,7,8], 3, "mouse"]
Consider the above list and answer the following:
a. L[3:]
b. L[::2]
c. L[1:2]
d. L[1][1]
e. Appropriate list method to :
i. Add elements at the end of a list.
ii. Get the position of an item in the list.
Answers
Answered by
14
Given list:
L = ["abc", [6, 7, 8], 3, "mouse"]
Output for a - d:
a. L[3:]
- ['mouse']
b. L[::2]
- ['abc', 3]
c. L[1:2]
- [[6, 7, 8]]
d. L[1][1]
- 7
The above act is known as list slicing. It can be performed if one is aware of each element's index positions.
e.
- (i). append() is a method that inserts an element at the end of the list. extend() could also be used, but append() takes in only one argument, meaning that append() can only insert one element at a time. extend() can take in multiple elements and all those elements will be stored at the last.
- (ii). index() is a method to retrieve the index position of the specified element.
Similar questions