Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Science, Class 12, CBSE
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
A linear list consists of a finite number of homogeneous data elements, i.e., data elements of the same data type, arranged in a sequential manner under one name.
(b)
Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
Explanation
A nested list is a list having one or more lists as its elements. Nested lists can be elements within other data structures like dictionaries or tuples. But nested lists can exist independently and are commonly used in Python programming to represent multi-dimensional lists.
(b)
Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
Explanation
A regular two dimensional list is a list having lists as its elements and each element-list has the same number of elements. Regular 2D lists do not necessarily depend on being nested within other data structures such as tuples or dictionaries because regular 2D lists can exist independently as data structures in Python.
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
A list that has lists as its elements, with each element-list having a different shape, i.e., a different number of elements, is a ragged 2D list, also known as an irregular 2D list.
(i) raw data — Raw data are raw facts. These are simply values or set of values.
(ii) data item — Data item represents single unit of values of certain type.
(iii) data type — A data type defines a set of values along with well-defined operations stating its input-output behaviour.
(iv) data structure — A data structure is a named group of data of different data types which is stored in a specific way and can be processed as a single unit. A data structure has well-defined operations, behaviour and properties.
(i) Simple data structures — Simple data structures are normally built from primitive data types like integers, reals, characters, boolean. For example : Array or Linear lists.
(ii) Compound data structures — Compound data structures are formed by combination of Simple data structures. The compound data structures may be linear (a single sequence) and non-linear (multi-level).
(iii) Linear data structures — A data structure is said to be linear if its elements form a sequence or linear list. These data structures are single level data structures. For example: Arrays.
(iv) Non-linear data structures — A data structure is said to be non-linear if traversal of nodes is nonlinear in nature. These are multilevel data structures. For example: Tree.
If the array is unsorted and the number of elements in the array are less then we should prefer linear search because the overhead of sorting the array will be more than traversing the complete array for finding the required element.
The conditions when binary search is applicable are as follows:
[10, 23, 56, [95, 10]]
Reason —
a = [10, 23, 56, [78, 10]]
— This creates a nested list a
.b = list(a)
— This creates a shallow copy of a
and assigns it to b
. The list b
references to the same objects as a
.a[3][0] += 17
— a[3]
accesses the sublist at index 3, i.e., [78, 10], and [0]
further accesses the first element of that sublist, i.e., 78. Then, it adds 17 to 78, changing it to 95.print(b)
— This prints the contents of list b
. Since b
is a shallow copy of a
, any changes made to the nested list within a
will also reflect in b
. Therefore, when we print b
, we'll see the modified value [95, 10].[('H', 1), ('E', 1), ('L',1), ('L',1), ('O', 1)]
Reason —
lst1 = "hello"
— This line initializes a string variable lst1
with the value "hello".lst2 = list((x.upper(), len(x)) for x in lst1)
— This line is a list comprehension, which iterates over each character x
in the string lst1
. For each character x
, it creates a tuple containing two elements:x
, obtained using the upper()
method.x
, obtained using the len()
function.lst2
.print(lst2)
— This line prints the list lst2
.[3, 7, 8, 6, 1, 2]
Reason —
lst = [3, 4, 6, 1, 2]
— This line initializes a list variable lst
with the values [3, 4, 6, 1, 2].lst[1:2] = [7,8]
— lst[1:2]
refers to a slice of list lst
at index 1. It replaces this slice with the values from the list [7, 8]
. Now lst
becomes [3, 7, 8, 6, 1, 2]
.lst[1] = [7, 8]
, then output would be [3, [7, 8], 6, 1, 2]
because it would replace the entire element at index 1 (i.e., 4) with [7, 8]
.)print(lst)
— This line prints the modified list lst
.What will be the output of the following Python code snippet ?
k = [print(i) for i in my_string if i not in "aeiou"]
prints all characters of my_string that aren't vowels
Reason — The expression [print(i) for i in my_string if i not in "aeiou"]
is a list comprehension that iterates over each character i
in the string my_string
. It checks if the character i
is not a vowel (i.e., it's not in the string "aeiou"). If i
is not a vowel, it prints the character i
.
Which of the following is the correct expansion of list_1 = [expr(i) for i in list_0 if func(i)] ?
(a)
list_1 = []
for i in list_0:
if func(i):
list_1.append(i)
(b)
for i in list_0:
if func(i):
list_1.append(expr(i))
(c)
list_1 = []
for i in list_0:
if func(i):
list_1.append(expr(i))
(d) none of these
list_1 = []
for i in list_0:
if func(i):
list_1.append(expr(i))
Reason —
for i in list_0
— This part iterates over each element i
in the list list_0
.if func(i)
— This part applies the function func()
to each element i
and checks if the result is True. Only elements for which func(i)
returns True will be included in the resulting list.expr(i)
— This part applies some expression or function call expr()
to each selected element i
.Therefore, the correct expansion would be:
list_1 = []
for i in list_0:
if func(i):
list_1.append(expr(i))
True
Reason — Queues data structures are FIFO (First In First Out) lists, where insertions take place at the "rear" end of the queues and deletions take place at the "front" end of the queues.
A data structure is a named group of data of different data types which is stored in a specific way and can be processed as a single unit. A data structure has well-defined operations, behaviour and properties.
Some common data structures are Stack, Lists, Queue, Linked lists and Tree.
Yes, data structures are related to data types, but they are distinct concepts. A data type is a set of values with well-defined operations dictating its input-output behavior e.g., two strings cannot be multiplied. While a data structure is a named group of data of different data types stored in a specific way, capable of being processed as a single unit e.g., lists, arrays, stack. A data structure possesses well-defined operations, behavior, and properties. Data structures not only allow users to combine various data types into a group but also enable processing of the group as a single unit, thereby making things much simpler and easier.
Linear data structures — A data structure is said to be linear if its elements form a sequence. These data structures are single level data structures. For example: Stack, Queue, Linked List.
Non-linear data structures — These data structures are multilevel data structures having a hierarchical relationship among its elements called nodes. For example: Tree
Stack, queue and linked list are linear data structures. Yes, a linked list is a linear data structure. Linked lists consist of a sequence of elements, each containing a reference to the next element in the sequence, forming a linear arrangement.
The working principle of a stack follows the Last In First Out (LIFO) technique, where insertions and deletions occur at one end, known as the top. Elements are added to and removed from the top of the stack, allowing the most recently added item to be accessed first.
The working principle of a queue follows the First In First Out (FIFO) technique, where insertions are made at one end, known as the rear, and deletions occur at the other end, known as the front. This ensures that the oldest elements in the queue are processed first, similar to waiting in a line or queue.
A linear list data structure is a list of finite number of data elements of the same data type arranged in a sequential manner under one name. The operations that we can perform on linear lists are as follows:
(i) Linear lists — Linear lists are utilized when storing and managing collections of data elements in a sequential manner is necessary. For example, managing a list of student records in a university database.
(ii) Stacks — Stacks are used when data needs to be managed based on the Last In, First Out (LIFO) principle. For example, function call management in programming languages employs a stack structure to store and manage function calls.
(iii) Queues — Queues are used when data needs to be managed based on the First In, First Out (FIFO) principle. For example, a print spooling system, where print jobs are queued in the order they were received.
A list comprehension is a concise description of a list that shorthands the list creating for loop in the form of a single statement. The syntax is:
[expression_creating_list for (set of values to iterate upon) condition]
List comprehensions make code compact, more readable, more efficient and are faster to execute.
Advantages of list comprehensions are as follows:
List comprehensions are used in situations where we need to perform simple operations on iterable data structures, leading to more readable and compact code. List comprehensions are commonly used for tasks such as generating sequences, mapping elements of a list, filtering elements based on certain criteria, and combining elements from multiple iterables.
List comprehensions should not be used when we need to check multiple conditions.
A two dimensional list is a list having all its elements as lists of same shapes, i.e., a two dimensional list is a list of lists. A two dimensional list is also a nested list, as it involves nesting one list inside another.
A regular two dimensional list is used where structured data organization is required. For example, a program to store and manipulate student grades. We can use a two-dimensional list where each row represents a student and each column represents a different subject.
A list that has lists as its elements, with each element-list having a different shape, i.e., a different number of elements, is a ragged list. These are also two dimensional lists but irregular 2D lists. So, the main difference between ragged lists and two-dimensional lists is that ragged lists have inner lists with varying lengths, while two-dimensional lists have inner lists with the same lengths, resulting in a regular structure.
Suppose we are building a program to store information about different families and their members. In this program, each family can have a variable number of members, making it suitable to use a ragged list.
Lists in Python are stored similarly to strings in memory. However, lists store references to their elements at each index, which can vary in size. This means that each index of a list holds a reference to the actual object stored elsewhere in memory. Each of the individual items in the list occupies its own memory location.
When it comes to 2D lists, also known as lists of lists, they follow a similar principle. Internally, a 2D list is represented as an array of pointers to inner lists. Each element of the outer list is a reference to an inner list object, which in turn is another array of pointers to the elements it contains.
Create a list SqLst that stores the doubles of elements of another list NumLst. Following code is trying to achieve this. Will this code work as desired ? What will be stored in SqLst after following code ?
NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = NumLst * 2
No, the above code will not work as desired.
[2, 5, 1, 7, 3, 6, 8, 9, 2, 5, 1, 7, 3, 6, 8, 9] will be stored in SqLst after the above code is executed. The multiplication operator * with a list NumLst
, duplicates the elements of the list NumLst
by 2 times.
The correct code is:
NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = []
for num in NumLst:
SqLst.append(num * 2)
print(SqLst)
[4, 10, 2, 14, 6, 12, 16, 18]
NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = []
for num in NumLst:
SqLst.append(num * 2)
print(SqLst)
[4, 10, 2, 14, 6, 12, 16, 18]
NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
— This line creates a list called NumLst
containing the given numbers [2, 5, 1, 7, 3, 6, 8, 9].SqLst = []
— This line initializes an empty list called SqLst
.for num in NumLst:
— This line starts a for
loop that iterates over each element num
in the NumLst
list.SqLst.append(num * 2)
— Inside the loop, each num
is multiplied by 2, and the result is appended to the SqLst
list.print(SqLst)
— This line prints the resulting list SqLst
.ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
A = [i for i in ML if i % 2 == 0]
print(A)
[4, 16, 36, 64, 100]
ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
— This line initializes a list ML
.A = [i for i in ML if i % 2 == 0]
— This line uses a list comprehension to create a new list A
. It iterates over each element i
in the list ML
, and only includes i
in A
if i % 2 == 0 is true. This condition checks if the element i
is even.print(A)
— This line prints the list A, which contains only the even numbers from the list ML
.In the above code, Python would raise an error because round brackets are used in list comprehension. List comprehensions work with square brackets only.
The corrected code is:
gen = [i/2 for i in [0, 9, 21, 32]]
print(gen)
The equivalent for loop for the above list comprehension is:
gen = []
for i in [0, 9, 21, 32]:
gen.append(i/2)
print(gen)
Enter a list : [12, 3, 4, 5, 7, 12, 8, 23, 12]
False
Enter a list : [8, 9, 2, 3, 7, 8]
True
s = eval(input("Enter a list : "))
— This line prompts the user to enter a list.n = len(s)
— This line finds the length of the list s
.t = s[1:n-1]
— This line slices the list s
starting from index 1 up to index n-1, which effectively removes the first and last elements from the list s
and stores it in list t
.print(s[0] == s[n-1] and t.count(s[0]) == 0)
— The condition s[0] == s[n-1] and t.count(s[0]) == 0
checks if the first and the last element of the list s
are same [s[0] == s[n-1]
] and that element does not appear anywhere else in list s
apart from the first and last position [t.count(s[0]) == 0
]. For case (i), 12 is the first and the last element of list but as it also occurs at the 5th index hence the output is False
. For case (ii), 8 is the first and the last element of list and it doesn't appear anywhere else hence the output is True
.Predict the output :
def h_t(NLst):
from_back = NLst.pop()
from_front = NLst.pop(0)
NLst.append(from_front)
NLst.insert(0, from_back)
NLst1 = [[21, 12], 31]
NLst3 = NLst1.copy()
NLst2 = NLst1
NLst2[-1] = 5
NLst2.insert(1, 6)
h_t(NLst1)
print(NLst1[0], NLst1[-1], len(NLst1))
print(NLst2[0], NLst2[-1], len(NLst2))
print(NLst3[0], NLst3[-1], len(NLst3))
5 [21, 12] 3
5 [21, 12] 3
[21, 12] 31 2
def h_t(NLst):
— This line defines a function named h_t
that takes a single argument NLst
.from_back = NLst.pop()
— This line removes and returns the last element from the list NLst
, storing it in the variable from_back
.from_front = NLst.pop(0)
— This line removes and returns the first element from the list NLst
, storing it in the variable from_front
.NLst.append(from_front)
— This line appends the value stored in from_front
to the end of the list NLst
.NLst.insert(0, from_back)
— This line inserts the value stored in from_back
at the beginning of the list NLst
.NLst1 = [[21, 12], 31]
— This line initializes NLst1
as a nested list.NLst3 = NLst1.copy()
— This line creates a shallow copy of NLst1
and assigns it to NLst3
.NLst2 = NLst1
— This line assigns the reference of NLst1
to NLst2
, meaning both NLst1
and NLst2
point to the same list object.NLst2[-1] = 5
— This line modifies the last element of NLst2
to 5. Since NLst1
and NLst2
reference the same list object, this change also affects NLst1
.NLst2.insert(1, 6)
— This line inserts 6 at index 1 in NLst2
. Again, since NLst1
and NLst2
reference the same list object, this change also affects NLst1
.h_t(NLst1)
— This line calls the function h_t
with NLst1
as an argument.print(NLst1[0], NLst1[-1], len(NLst1))
— This line prints the first element, last element, and length of NLst1
.print(NLst2[0], NLst2[-1], len(NLst2))
— This line prints the first element, last element, and length of NLst2
.print(NLst3[0], NLst3[-1], len(NLst3))
— This line prints the first element, last element, and length of NLst3
.The above code will raise an error due to an indentation error in line 2.
The correct code is:
ages = [11, 14, 15, 17, 13, 18, 25]
print(ages)
Elig = [x for x in ages if x in range(14, 18)]
print(Elig)
[11, 14, 15, 17, 13, 18, 25]
[14, 15, 17]
ages = [11, 14, 15, 17, 13, 18, 25]
— This line initializes a list ages
.print(ages)
— This line prints the list ages
.Elig = [x for x in ages if x in range(14, 18)]
— This line uses list comprehension to create new list Elig
, by taking the values between 14 to 17 from ages
list.print(Elig)
— This line prints the list Elig
.[0, 9, 36, 81, 4]
[0, 9, 36, 81, 4]
[0, 9, 36, 81]
L1 = [x ** 2 for x in range(10) if x % 3 == 0]
— This line creates a list L1
containing the squares of multiples of 3 between 0 to 9.L2 = L1
— This line assigns list L1
to list L2
, meaning both L1
and L2
reference the same list in memory (shallow copy).L1.append(len(L1))
— This line appends the length of L1
to L1
. So, the length of L1
is 4, and it appends 4 to L1
.print(L1)
— This prints the modified L1
list.print(L2)
— This prints L2
, which is pointing to the same list as L1
.L2.remove(len(L2) - 1)
— This line removes the last element of L2
since len(L2) - 1
gives the last index of L2
.print(L1)
— This prints L1
again. Since both L1
and L2
reference the same list, modifying L2
also affects L1
. Therefore, L1
also reflects the change made to L2
.[2, 4, 6, 8]
def even(n)
— This line defines a function named even
that takes a parameter n
.return n % 2 == 0
— This line returns True if the input number n
is even (i.e., when n % 2 equals 0), and False otherwise.list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
— This line initializes a list named list1
with integers from 1 to 9.ev = [n for n in list1 if n % 2 == 0]
— This line creates a new list ev
using a list comprehension. It iterates over each element n
in list1
and includes n
in ev
if it's even (i.e., if n % 2 == 0).evp = [n for n in list1 if even(n)]
— This line creates a new list evp
using a list comprehension. It iterates over each element n
in list1
and includes n
in evp
if the function even(n)
returns True for that n
. Effectively, this also creates a new list from the even numbers of list1
. Therefore, both ev
and evp
will contain same values.print(evp)
— This line prints the list evp
.[[9, 6], [4, 5], 10]
b = [[9, 6], [4, 5], [7, 7]]
— This line initializes a list b
containing three sublists, each containing two elements.x = b[:2]
— This line creates a new list x
by slicing the list b
from index 0 to index 1. So, x
will contain the first two sublists of b
. At this point, x
will be [[9, 6], [4, 5]]
.x.append(10)
— This line appends the integer 10 to the end of the list x
. x
now becomes [[9, 6], [4, 5], 10]
.print(x)
— This line prints the list x
.[[9, 6], [4, 5, 10]]
b = [[9, 6], [4, 5], [7, 7]]
— This line initializes a list b
containing three sublists, each containing two elements.x = b[:2]
— This line creates a new list x
by slicing the list b
from index 0 to index 1. So, x
will contain the first two sublists of b
. At this point, x
will be [[9, 6], [4, 5]]
.x[1].append(10)
— This line accesses the second sublist of x
, which is [4, 5]
, and appends 10 at its end. Now x
becomes [[9, 6], [4, 5, 10]]
print(x)
— This line prints the list x
.Find the Error. Consider the following code, which runs correctly at times but gives error at other times. Find the error and its reason.
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
print("List1 originally is: ", Lst1)
ch = int(input("Enter 1/2/3 and \
predict which operation was performed?"))
if ch == 1:
Lst1.append(100)
Lst2.append(100)
elif ch == 2:
print(Lst1.index(100))
print(Lst2.index(100))
elif ch == 3:
print(Lst1.pop())
print(Lst2.pop())
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
print("List1 originally is: ", Lst1)
ch = int(input("Enter 1/2/3 and \
predict which operation was performed?"))
if ch == 1:
Lst1.append(100)
Lst2.append(100)
elif ch == 2:
print(Lst1.index(100)) # Error 1
print(Lst2.index(100)) # Error 2
elif ch == 3:
print(Lst1.pop())
print(Lst2.pop()) # Error 3
When the user selects option 1 (ch == 1), the code works correctly, i.e., it appends 100 to both Lst1 and Lst2 at the end. However, errors occur when the user selects option 2 (ch == 2) or option 3 (ch == 3). The errors are as follows:
Lst1
using Lst1.index(100)
, an error occurs because 100 is not present in Lst1
.Lst2
using Lst2.index(100)
, an error occurs because 100 is not present in Lst2
.Lst2
using Lst2.pop()
, an error occurs because there are no items to remove.Lst1
and Lst2
, we can use conditional checks to verify if 100 is present in each list before attempting to find its index. If 100 is not found, we print an error message.Lst2
, we use conditional checks to verify if the list is empty before attempting to pop elements. If a list is empty, we print an error message indicating that popping is not possible.The corrected code is :
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
print("List1 originally is: ", Lst1)
ch = int(input("Enter 1/2/3 and predict which operation was performed? "))
if ch == 1:
Lst1.append(100)
Lst2.append(100)
elif ch == 2:
if 100 in Lst1:
print("Index of 100 in Lst1:", Lst1.index(100))
else:
print("Error: 100 is not in Lst1")
if 100 in Lst2:
print("Index of 100 in Lst2:", Lst2.index(100))
else:
print("Error: 100 is not in Lst2")
elif ch == 3:
print(Lst1.pop())
if Lst2:
Lst2.pop()
else:
print("Error: Cannot pop from an empty list (Lst2)")
if
statements within the list comprehension. We should use a single if
statement with both conditions combined using the and
operator.if
statements within the list comprehension. To resolve this error, we should use a single if
statement with both conditions combined using the and
operator.(i)
y for y in range(100) if y % 2 == 0 and if y % 5 == 0
The list comprehension should be enclosed in square brackets, and we should use a single if
statement with both conditions combined using the and
operator.
The corrected code is:
[y for y in range(100) if y % 2 == 0 and y % 5 == 0]
(ii)
(y for y in range(100) if y % 2 == 0 and if y % 5 == 0)
The list comprehension should be enclosed in square brackets, and we should use a single if
statement with both conditions combined using the and
operator.
The corrected code is:
[y for y in range(100) if y % 2 == 0 and y % 5 == 0]
(iii)
["good" if i < 3: else: "better" for i in range(6)]
The list comprehension does not include colon.
The corrected code is:
["good" if i < 3 else "better" for i in range(6)]
Write a program that uses a function called find_in_list() to check for the position of the first occurrence of v in the list passed as parameter (lst) or -1 if not found. The header for the function is given below :
def find_in_list(lst, v):
""" lst - a list
v - a value that may or
may not be in the list """
def find_in_list(lst, v):
if v in lst:
return lst.index(v)
else:
return -1
lst = eval(input("Enter a list : "))
v = int(input("Enter a value to be checked: "))
print(find_in_list(lst, v))
Enter a list : [1, 2, 4, 7, 2, 55, 78]
Enter a value to be checked: 2
1
Enter a list : [10, 30, 54, 58, 22]
Enter a value to be checked: 22
4
Enter a list : [9, 5, 3, 33]
Enter a value to be checked: 10
-1
Implement the following function for a linear list, which find outs and returns the number of unique elements in the list
def unique(lst):
"""passed parameter lst is a list of
integers (could be empty)."""
After implementing the above function, test it with following lists and show the output produced by above function along with the reason for that output.
(i) lst = []
(ii) lst = [1, 2, 3]
(iii) lst = [1, 2, 2]
(iv) lst = [1, 2, 2, 3, 3]
def unique(lst):
c = 0
for i in range(0, len(lst)):
if lst[i] not in lst[i+1:]:
c += 1
return c
lst = eval(input("Enter the list: "))
print(unique(lst))
Enter the list: []
0
Enter the list: [1, 2, 3]
3
Enter the list: [1, 2, 2]
2
Enter the list: [1, 2, 2, 3, 3]
3
(i) lst = [] — The output is 0 because the list is empty.
(ii) lst = [1, 2, 3] — The output is 3 because the list contains 3 unique elements — 1, 2, 3.
(iii) lst = [1, 2, 2] — The output is 2 because the list contains 2 unique elements — 1, 2.
(iv) lst = [1, 2, 2, 3, 3] — The output is 3 because the list contains 3 unique elements — 1, 2, 3.
Use a list comprehension to create a list, CB4. The comprehension should consist of the cubes of the numbers 1 through 10 only if the cube is evenly divisible by four. Finally, print that list to the console. Note that in this case, the cubed number should be evenly divisible by 4, not the original number.
CB4 = [x**3 for x in range(1, 11) if (x**3) % 4 == 0]
print(CB4)
[8, 64, 216, 512, 1000]
Take two lists, say for example these two :
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. Write this in one line of Python using at least one list comprehension. Run the complete program and show output.
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = [a[i] for i in range(len(a)) if a[i] in b and a[i] not in a[i+1:]]
print (c)
[1, 2, 3, 5, 8, 13]
Suppose we have a list V where each element represents the visit dates for a particular patient in the last month. We want to calculate the highest number of visits made by any patient. Write a function MVisit(Lst) to do this.
A sample list (V) is shown below :
V = [[2, 6], [3, 10], [15], [23], [1, 8, 15, 22, 29], [14]]
For the above given list V, the function MVisit() should return the result as [1, 8, 15, 22, 29].
def MVisit(Lst):
max_index = 0
for i in range(1, len(Lst)):
if len(Lst[i]) > len(Lst[max_index]):
max_index = i
return Lst[max_index]
V = [[2, 6], [3, 10], [15], [23], [1, 8, 15, 22, 29], [14]]
result = MVisit(V)
print(result)
[1, 8, 15, 22, 29]