Q. Create a list L containing first 10 even numbers. Write statements (one statement per task) for the following:
(a) | Change elements from index 4 to 9 to first 5 odd numbers in single statement. | |
(b) | Add the replaced even numbers to the end of the list. | |
(c) | Remove the middle element of the list. If two elements are at the middle of a list, delete the second one. | |
(d) | Sort the list in reverse order. |
Answer :-
Let, a = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
list, b = a + [10, 12, 14, 16, 18, 20]
(a) | Change elements from index 4 to 9 to first 5 odd numbers in single statement. | a [4:10] = [1, 3, 5, 7, 9] |
(b) | Add the replaced even numbers to the end of the list. | b = a + [10, 12, 14, 16, 18, 20] |
(c) | Remove the middle element of the list. If two elements are at the middle of a list, delete the second one. | b.pop(int(len(b)/2)) |
(d) | Sort the list in reverse order. | b.sort(reverse = True) OR >>> b.sort() >>> b.reverse() |
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )