Lists in Python

ICSE Class 10 Robotics & AI · Exploring Robotics & AI · 32 solved questions

Exercise
Marks
11 Mark · Multiple Choice Questions

What does the following pic depict?

What does the following pic depict. Lists in Python, APC ICSE Robotics & Artificial Intelligence Solutions Class 10.
Show SolutionHide Solution↓

List Representation

Reason — The picture shows elements enclosed within square brackets [ ], which represents a List in Python. It also displays both forward (positive) indexing starting from 0 and backward (negative) indexing starting from –1, which are features of List Indexing.

21 Mark · Multiple Choice Questions

Which of the following is returned by using list() function?

Show SolutionHide Solution↓

An empty list

Reason — The list() function, when used without any argument, creates and returns an empty list.

31 Mark · Multiple Choice Questions

Given: L = ['a', 'b', 'c', 'd']

Which of the following list slice will produce ['b', 'c', 'd']?

Show SolutionHide Solution↓

L[1:4]

Reason — In list slicing, the syntax is L[initial index : final index], where elements are extracted from the initial index up to (final index − 1).

For L = ['a', 'b', 'c', 'd']:

  • L[1:4] extracts elements from index 1 to 3.
  • Index 1 → 'b'
  • Index 2 → 'c'
  • Index 3 → 'd'

Thus, the result is ['b', 'c', 'd'].

41 Mark · Multiple Choice Questions

Which of the following represents a Python list?

Show SolutionHide Solution↓

[1, 2, 3]

Reason — A Python list is represented by elements separated by commas and enclosed within square brackets [ ]. Therefore, [1, 2, 3] is a valid list representation.

51 Mark · Multiple Choice Questions

What will the following relation result in?

[1, 2, 3, 4] > [1, 2, 8, 9]

Show SolutionHide Solution↓

False

Reason — In list comparison, corresponding elements are compared starting from the 0th index, and the result is determined as soon as unequal elements are found.

Comparison:

  • 1 = 1
  • 2 = 2
  • 3 < 8

Since the first unequal elements are 3 and 8, and 3 is less than 8, the relation [1, 2, 3, 4] > [1, 2, 8, 9] becomes False.

61 Mark · Multiple Choice Questions

Which of the following functions will concatenate the two lists together?

Show SolutionHide Solution↓

extend()

Reason — The extend() function is used to add multiple elements of one list to another list, effectively concatenating two lists. The append() function adds only a single element, while merge() and join() are not list functions.

1State True or False

State whether the following statements are True or False:

Show SolutionHide Solution↓
  1. True
  2. True
  3. False
    Corrected Statement: In forward indexing, the indexing of the last element is (length − 1).
  4. True
  5. True
  6. False
    Corrected Statement: The remove() function is used to delete an element by its value from the list.
1Name the Following

Name the built-in list function for the following tasks.

Show SolutionHide Solution↓
  1. del
  2. extend()
  3. count()
  4. append()
  5. sort()
1Assertion and Reason Based Question

Assertion (A): You can compare two lists together by using relational operators.

Reason (R): The relational operators such as ==, <, <=, >, >=, etc. are used to compare the list elements with each other starting with the 0th index. The result of comparison will either be true or false.

Based on the above discussion, choose an appropriate statement from the options given below:

Show SolutionHide Solution↓

Both A and R are true and R is the correct explanation of A.

Reason — We can compare two lists together by using relational operators (i.e., ==, <, <=, >, >=, etc.). During the process, the corresponding elements of both the lists are compared with each other starting with the 0th index. The result is determined the moment elements are found different. In case, all the corresponding elements are same, both the lists are declared to be the same. The result of comparison will either be true or false.

1Application Based Question

After learning the lesson, you are expected to answer the following list operations. The two lists are shown below:

L1 = ['a', 'e', 'i', 'o', 'u']
L2 = [12, 13, 14]

Predict the output for the following snippets:

(a)

L3 = L1.extend(L2)
print(L3)

(b)

L3 = L1[1:4] + L2[::]
print(L3)

(c)

del L1[1:3]
print(L1)

(d)

print(2*L2)
Show SolutionHide Solution↓

(a)

None

The extend() function is used to add multiple elements within another list. It adds the elements of L2 to L1, but it does not return a new list. Since the returned value is assigned to L3, the value of L3 becomes None.

(b)

['e', 'i', 'o', 12, 13, 14]

Step 1: L1[1:4]

List slicing extracts elements from the initial index up to (final index − 1). Here, elements are extracted from index 1 to 3.

L1 = ['a', 'e', 'i', 'o', 'u']

Index positions:
0 → 'a'
1 → 'e'
2 → 'i'
3 → 'o'
4 → 'u'

So, L1[1:4] gives: ['e', 'i', 'o']

Step 2: L2[::]

When no initial and final index is given, all elements of the list are extracted.

L2 = [12, 13, 14]

So, L2[::] gives: [12, 13, 14]

Step 3: + Operator

The + operator is used for concatenation, i.e., to join two lists together to form a single list. Therefore, ['e', 'i', 'o'] + [12, 13, 14] = ['e', 'i', 'o', 12, 13, 14].

(c)

['a', 'o', 'u']

The del keyword is used to delete elements from a list either at a specified index or within a range of indexes. When a range is given, elements from the initial index up to (final index − 1) are deleted. Here, del L1[1:3] deletes elements from index 1 to 2, i.e., 'e' and 'i' from the list ['a', 'e', 'i', 'o', 'u']. Hence, the remaining list becomes ['a', 'o', 'u'].

(d)

[12, 13, 14, 12, 13, 14]

List replication is the system of duplicating the elements of the list (i.e., appending the list with the elements of the same list). It is done with the help of the * operator. Therefore, 2*L2 duplicates the elements of the list [12, 13, 14] and results in [12, 13, 14, 12, 13, 14].

2(i)Application Based Question

Predict the output of the following list operation:

print([2, 4, 6, 8] < [5, 9])
Show SolutionHide Solution↓
True

In list comparison, the corresponding elements of both the lists are compared with each other starting with the 0th index. The result is determined the moment elements are found different. Here, the first elements are compared: 2 and 5. Since 2 is less than 5, the expression [2, 4, 6, 8] < [5, 9] becomes True.

2(ii)Application Based Question

Predict the output of the following list operation:

print([1, 2, 8, 5] > [1, 2, 13, 5])
Show SolutionHide Solution↓
False

In list comparison, the corresponding elements of both the lists are compared with each other starting with the 0th index, and the result is determined the moment elements are found different.

Comparison:
1 = 1
2 = 2
8 < 13

Since 8 is less than 13, the expression [1, 2, 8, 5] > [1, 2, 13, 5] becomes False.

2(iii)Application Based Question

Predict the output of the following list operation:

L1 = [1, 3, 5, 3, 7, 3, 9]
c = L1.count(3)
print(c) 
Show SolutionHide Solution↓
3

The count() function is used to find the frequency of an element among a set of elements stored in the list. In the list [1, 3, 5, 3, 7, 3, 9], the value 3 occurs three times. Therefore, L1.count(3) returns 3.

2(iv)Application Based Question

Predict the output of the following list operation:

L1 = [23, 34, 51, 62, 45]
print(L1(1:4))
Show SolutionHide Solution↓

To access or slice elements from a list, the index or range of indexes must be enclosed within square brackets [ ] along with the list variable, as per the slicing syntax <List variable>[initial index : final index]. In the given code, parentheses ( ) are used instead of square brackets, i.e., L1(1:4), which is incorrect syntax for list slicing. Therefore, the statement results in an error.

2(v)Application Based Question

Predict the output of the following list operation:

L1 = [11, 15, 24, 38, 82] 
print(L1.pop(1) + L1.pop(3))
Show SolutionHide Solution↓
97

The pop(index) function is used to delete and return the element available at the given index of the list. First, L1.pop(1) deletes and returns the element at index 1, i.e., 15. The list becomes [11, 24, 38, 82]. Then, L1.pop(3) deletes and returns the element at index 3 of the updated list, i.e., 82. Therefore, the expression becomes 15 + 82, which results in 97.

1Programs on Python List

Write a Python code to accept integers in a list. Find and display the sum of even and odd numbers separately.

Show SolutionHide Solution↓
sum_even = 0
sum_odd = 0
Lst = eval(input("Enter list elements within []:"))

for i in range(len(Lst)):
    if(Lst[i] % 2 == 0):
        sum_even = sum_even + Lst[i]
    else:
        sum_odd = sum_odd + Lst[i]

print("Sum of even integers:", sum_even)
print("Sum of odd integers:", sum_odd)
Enter list elements within []:[12, 7, 9, 14, 6, 3]
Sum of even integers: 32
Sum of odd integers: 19
2Programs on Python List

Write a Python code to accept integers in a list. Find and display the sum and product of the numbers available at even and odd indexes of the list respectively.

Show SolutionHide Solution↓
sum_even_index = 0
prod_odd_index = 1
L1 = eval(input("Enter list elements within [ ]:"))

for i in range(len(L1)):
    if(i % 2 == 0):
        sum_even_index = sum_even_index + L1[i]
    else:
        prod_odd_index = prod_odd_index * L1[i]

print("Sum of elements at even indexes:", sum_even_index)
print("Product of elements at odd indexes:", prod_odd_index)
Enter list elements within [ ]:[5, 4, 3, 2, 6, 7]
Sum of elements at even indexes: 14
Product of elements at odd indexes: 56
3Programs on Python List

Write a Python code to enter a set of numbers in a list. Enter a number and search in the given list. If it is found then display "Search successful" otherwise display "No such number is found in the list".

Show SolutionHide Solution↓
L1 = eval(input("Enter a set of numbers in the list within [ ]:"))

num = eval(input("Enter an element to be searched:"))
flag = False
p = len(L1)

for a in range(p):
    if(num == L1[a]):
        flag = True
        break

if(flag == True):
    print("Search successful")
else:
    print("No such number is found in the list")
Enter a set of numbers in the list within [ ]:[21, 34, 45, 28, 91, 75]
Enter an element to be searched:91
Search successful
4Programs on Python List

Write a code in Python to accept a set of names in a list. Sort the names in alphabetical order by using sort() function. Finally, display the sorted names.

Show SolutionHide Solution↓
L1 = eval(input("Enter names in the list within [ ]:"))
L1.sort()
print("Names in alphabetical order:", L1)
Enter names in the list within [ ]:['Riya', 'Ankit', 'Meera', 'Dev']
Names in alphabetical order: ['Ankit', 'Dev', 'Meera', 'Riya']
5Programs on Python List

Write a Python code to accept elements in two different lists. Create third list that should contain all the elements of first list followed by the elements of second list. Finally, display the elements of the third list.

Show SolutionHide Solution↓
L1 = eval(input("Enter elements of first list within [ ]:"))
L2 = eval(input("Enter elements of second list within [ ]:"))
L3 = L1 + L2
print("Elements of the third list:", L3)
Enter elements of first list within [ ]:[1, 3, 5]
Enter elements of second list within [ ]:[2, 4, 6]
Elements of the third list: [1, 3, 5, 2, 4, 6]
6Programs on Python List

Write a program in Python to accept a set of integers in a list. Display only those numbers which are composite.

Hint: A number is said to be composite, if it has more than two factors including 1 (one) and the number itself.

Show SolutionHide Solution↓
L1 = eval(input("Enter numbers in the list within [ ]:"))
print("The composite numbers are:")
for a in L1:
    c = 0
    for b in range(1, a+1):
        if(a % b == 0):
            c = c + 1
    if(c > 2):
        print(a)
Enter numbers in the list within [ ]:[21, 45, 29, 37, 82, 31]
The composite numbers are:
21
45
82
1Answer the Following Questions

Define a list with an example.

Show SolutionHide Solution↓

A list is defined as an ordered sequence of data (may be same or different data types) separated by commas and enclosed within opening and closing square brackets [ ].

For example: Lst = [1, 3, 5, 7, 9]

2Answer the Following Questions

What is a null or empty list?

Show SolutionHide Solution↓

An empty or null list is a list that does not enclose any value under opening and closing square brackets [ ].

For example: Lst = [ ]

3Answer the Following Questions

Can a list contain data of different types? Justify.

Show SolutionHide Solution↓

Yes, a list can contain data of different types. A list is defined as an ordered sequence of data (may be same or different data types) separated by commas and enclosed within opening and closing square brackets [ ].

For example: ["Robotics", "ICSE", 2025]

4Answer the Following Questions

What is meant by list slice?

Show SolutionHide Solution↓

List slicing is a technique to create a list after ignoring some elements of the existing list.

5Answer the Following Questions

Differentiate between del and pop() functions.

Show SolutionHide Solution↓

Differences between del and pop() functions are:

delpop()
This statement deletes a single element as well as a set of elements from the list.This function deletes a single element from the list.
It does not return the element(s) deleted from the list.It returns the element deleted from the list.
6(a)Answer the Following Questions

Define len() function.

Show SolutionHide Solution↓

The len() function is used to find the length of a list (i.e., the number of elements available in the list).

6(b)Answer the Following Questions

Define extend() function.

Show SolutionHide Solution↓

The extend() function is used to add multiple elements within another list. It is also called merging the elements of two lists.

6(c)Answer the Following Questions

Define sort() function.

Show SolutionHide Solution↓

The sort() function is used to arrange the list of elements in a specific order (ascending or descending).

6(d)Answer the Following Questions

Define insert() function.

Show SolutionHide Solution↓

The insert() function is used to add an element at a specified index used as an argument to the function.

6(e)Answer the Following Questions

Define append() function.

Show SolutionHide Solution↓

The append() function is used to add an element at the end of an existing list of elements. It adds a single element to a list.

6(f)Answer the Following Questions

Define count() function.

Show SolutionHide Solution↓

The count() function is used to find the frequency of an element among a set of elements stored in the list.

Press ↓ to reveal a solution and move to the next question. Back to Exploring Robotics & AI.

Free sample bank