Computer Science
If L1 = [1, 2, 3, 2, 1, 2, 4, 2, …], and L2 = [10, 20, 30, …], then
(Answer using builtin functions only)
(a) Write a statement to count the occurrences of 4 in L1.
OR
(b) Write a statement to sort the elements of list L1 in ascending order.(a) Write a statement to insert all the elements of L2 at the end of L1.
OR
(b) Write a statement to reverse the elements of list L2.
Python List Manipulation
2 Likes
Answer
(a)
L1.count(4)
OR
(b)L1.sort()
(a)
L1.extend(L2)
OR
(b)L2.reverse()
Answered By
1 Like
Related Questions
How is a mutable object different from an immutable object in Python ?
Identify one mutable object and one immutable object from the following:
(1, 2), [1, 2], {1:1, 2:2}, '123'
Give two examples of each of the following:
- Arithmetic operators
- Relational operators
Identify the correct output(s) of the following code. Also write the minimum and the maximum possible values of the variable b.
import random a = "Wisdom" b = random.randint(1, 6) for i in range(0, b, 2): print(a[i], end = '#')
- W#
- W#i#
- W#s#
- W#i#s#
The code provided below is intended to swap the first and last elements of a given tuple. However, there are syntax and logical errors in the code. Rewrite it after removing all errors. Underline all the corrections made.
def swap_first_last(tup) if len(tup) < 2: return tup new_tup = (tup[-1],) + tup[1:-1] + (tup[0]) return new_tup result = swap_first_last((1, 2, 3, 4)) print("Swapped tuple: " result)