List in Python

List in Python


A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [ ]. Each value of a list is called as element. It can be of any type such as numbers, characters, strings and even the nested lists as well. Th e elements can be modifi ed or mutable which means the elements can be replaced, added or removed. Every element rests at some position in the list. Th e position of an element is indexed with numbers beginning with zero which is used to locate and access a particular element.


Create a List in Python

In python, a list is simply created by using square bracket. Th e elements of list should be specifi ed within square brackets. Th e following syntax explains the creation of list.

Syntax:

Variable = [element-1, element-2, element-3 …… element-n]

Example

Marks = [10, 23, 41, 75]
Fruits = [“Apple”, “Orange”, “Mango”, “Banana”]
MyList = [ ]

In the above example, the list Marks has four integer elements; second list Fruits has four string elements; third is an empty list. The elements of a list need not be homogenous type of data. The following list contains multiple type elements.

Mylist = [ “Welcome”, 3.14, 10, [2, 4, 6] ]

In the above example, Mylist contains another list as an element. This type of list is known as “Nested List”. 

Nested list is a list containing another list as an element.


Accessing List elements

Python assigns an automatic index value for each element of a list begins with zero. Index value can be used to access an element in a list. In python, index value is an integer number which can be positive or negative.

Positive value of index counts from the beginning of the list and negative value means counting backward from end of the list (i.e. in reverse order).

To access an element from a list, write the name of the list, followed by the index of the element enclosed within square brackets.

Syntax:

List_Variable = [E1, E2, E3 …… En]
print (List_Variable[index of a element])

Example (Accessing single element):

>>> Marks = [10, 23, 41, 75]
>>> print (Marks[0])
10

In the above example, print command prints 10 as output, as the index of 10 is zero.

Example: Accessing elements in revevrse order

>>> Marks = [10, 23, 41, 75]
>>> print (Marks[-1])
75

* A negative index can be used to access an element in reverse order.


(i) Accessing all elements of a list

Loops are used to access all elements from a list. The initial value of the loop must be zero. Zero is the beginning index value of a list.

Example

Marks = [10, 23, 41, 75]
i = 0
while i < 4:
print (Marks[i])
i = i + 1

Output

10
23
41
75

(ii) Reverse Indexing

Python enables reverse or negative indexing for the list elements. Thus, python lists index in opposite order. The python sets -1 as the index value for the last element in list and -2 for the preceding element and so on. This is called as Reverse Indexing.

Example

Marks = [10, 23, 41, 75]
i = -1
while i >= -4:
print (Marks[i])
i = i + -1

Output

75
41
23
10

List Length

The len( ) function in Python is used to find the length of a list. (i.e., the number of elements in a list). Usually, the len( ) function is used to set the upper limit in a loop to read all the elements of a list. If a list contains another list as an element, len( ) returns that inner list as a single element.

Example :Accessing single element

>>> MySubject = [“Tamil”, “English”, “Comp. Science”, “Maths”]
>>> len(MySubject)
4

Example : Program to display elements in a list using loop

MySubject = ["Tamil", "English", "Comp. Science", "Maths"]
i = 0
while i < len(MySubject):
print (MySubject[i])
i = i + 1

Output

Tamil
English
Comp. Science
Maths


Accessing elements using for loop

In Python, the for loop is used to access all the elements in a list one by one. This is just like the for keyword in other programming language such as C++.

Syntax:

for index_var in list:
print (index_var)

Here, index_var represents the index value of each element in the list. Python reads this “for” statement like English: “For (every) element in (the list of) list and print (the name of the) list items”

Example

Marks=[23, 45, 67, 78, 98]
for x in Marks:
print( x )

Output

23
45
67
78
98

In the above example, Marks list has 5 elements; each element is indexed from 0 to 4. The Python reads the for loop and print statements like English: “For (every) element (represented as x) in (the list of) Marks and print (the values of the) elements”.

Changing list elements

In Python, the lists are mutable, which means they can be changed. A list element or range of elements can be changed or altered by using simple assignment operator (=).

Syntax:

List_Variable [index of an element] = Value to be changed
List_Variable [index from : index to] = Values to changed

Where, index from is the beginning index of the range; index to is the upper limit of the range which is excluded in the range. For example, if you set the range [0:5] means, Python takes only 0 to 4 as element index. Thus, if you want to update the range of elements from 1 to 4, it should be specified as [1:5].

Example : Python program to update/change single value

MyList = [2, 4, 5, 8, 10]
print ("MyList elements before update... ")
for x in MyList:
print (x)
MyList[2] = 6
print ("MyList elements after updation... ")
for y in MyList:
print (y)

Output:

MyList elements before update...
2
4
5
8
10
MyList elements after updation...
2
4
6
8
10


Example : Python program to update/change range of values

MyList = [1, 3, 5, 7, 9]
print ("List Odd numbers... ")
for x in MyList:
print (x)
MyList[0:5] = 2,4,6,8,10
print ("List Even numbers... ")
for y in MyList:
print (y)

Output

List Odd numbers...
1
3
5
7
9
List Even numbers...
2
4
6
8
10


Adding more elements in a list

In Python, append( ) function is used to add a single element and extend( ) function is used to add more than one element to an existing list.

Syntax:

List.append (element to be added)
List.extend ( [elements to be added])

In extend( ) function, multiple elements should be specified within square bracket as arguments of the function.

Example

>>> Mylist=[34, 45, 48]
>>> Mylist.append(90)
>>> print(Mylist)
[34, 45, 48, 90]

In the above example, Mylist is created with three elements. Through >>> Mylist.append(90) statement, an additional value 90 is included with the existing list as last element, following print statement shows all the elements within the list MyList.

Example

>>> Mylist.extend([71, 32, 29])
>>> print(Mylist)
[34, 45, 48, 90, 71, 32, 29]

In the above code, extend( ) function is used to include multiple elements, the print statement shows all the elements of the list after the inclusion of additional elements.


Inserting elements in a list

As you learnt already, append( ) function in Python is used to add more elements in a list. But, it includes elements at the end of a list. If you want to include an element at your desired position, you can

use insert ( ) function. The insert( ) function is used to insert an element at any position of a list.

Syntax:

List.insert (position index, element)

Example

>>> MyList=[34,98,47,'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan' ]
>>> print(MyList)
[34, 98, 47, 'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan']
>>> MyList.insert(3, 'Ramakrishnan')
>>> print(MyList)
[34, 98, 47, 'Ramakrishnan', 'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan']

In the above example, insert( ) function inserts a new element ‘Ramakrishnan’ at the index value 3, ie. at the 4th position. While inserting a new element in between the existing elements, at a particular location, the existing elements shifts one position to the right.

Deleting elements from a list

There are two ways to delete an element from a list viz. del statement and remove( ) function. del statement is used to delete known elements whereas remove( ) function is used to delete elements of a list if its index is unknown. The del statement can also be used to delete entire list.

Syntax:

del List [index of an element]
# to delete a particular element
del List [index from : index to]
# to delete multiple elements
del List
# to delete entire list

Example

>>> MySubjects = ['Tamil', 'Hindi', 'Telugu', 'Maths']
>>> print (MySubjects)
['Tamil', 'Hindi', 'Telugu', 'Maths']
>>> del MySubjects[1]
>>> print (MySubjects)
['Tamil', 'Telugu', 'Maths']

In the above example, the list MySubjects has been created with four elements. print statement shows all the elements of the list. In >>> del MySubjects[1] statement, deletes an element whose index value is 1 and the following print shows the remaining elements of the list.

Example

>>> del MySubjects[1:3]
>>> print(MySubjects)
['Tamil']

In the above codes, >>> del MySubjects[1:3] deletes the second and third elements from the list. The upper limit of index is specified within square brackets, will be taken as -1 by the python.

Example

>>> del MySubjects
>>> print(MySubjects)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
print(MySubjects)
NameError: name 'MySubjects' is not defined

List and range ( ) function

The range( ) is a function used to generate a series of values in Python. Using range( ) function, you can create list with series of values. The range( ) function has three arguments.

Syntax of range ( ) function:

range (start value, end value, step value)

where,

start value – beginning value of series. Zero is the default beginning value.

end value – upper limit of series. Python takes the ending value as upper limit – 1.

step value – It is an optional argument, which is used to generate different interval of values.

Example : Generating whole numbers upto 10

for x in range (1, 11):
print(x)

Output

1
2
3
4
5
6
7
8
9
10

Example : Generating first 10 even numbers

for x in range (2, 11, 2):
print(x)

Output

2
4
6
8
10

Creating a list with series of values

Using the range( ) function, you can create a list with series of values. To convert the result of range( ) function into list, we need one more function called list( ). The list( )

function makes the result of range( ) as a list.

Syntax:

List_Varibale = list ( range ( ) )

Example

>>> Even_List = list(range(2,11,2))
>>> print(Even_List)
[2, 4, 6, 8, 10]


Other important list funcion

Function
Description
Syntax
Example
copy ( )
Returns a copy of the list
List.copy( )

MyList=[12, 12, 36]
x = MyList.copy()
print(x)

Output:

[12, 12, 36]

count ( )
Returns the number of similar elements present in the last.
List.count(value)
MyList=[36 ,12 ,12]
x = MyList.count(12)
print(x)

Output:

2

index ( )
Returns the index value of the first recurring element
List.index(element)
MyList=[36 ,12 ,12]
x = MyList.index(12)
print(x)

Output:

0

reverse ( )
Reverses the order of the element in the list.
List.reverse( )
MyList=[36 ,23 ,12]
MyList.reverse()
print(MyList)

Output:

[12 ,23 ,36]

sort ( )
Sorts the element in list
List.sort(reverse=True|False, key=myFunc)

max( )
Returns the maximum value in a list.
max(list)
MyList=[21,76,98,23]
print(max(MyList))

Output:

98

min( )
Returns the minimum value in a list.
min(list)
MyList=[21,76,98,23]
print(min(MyList))

Output:

21

sum( )
Returns the sum of values in a list.
sum(list) 
MyList=[21,76,98,23]
print(sum(MyList))

Output:

218

Qus. 1 : <p>What will be the output of the following Python code?</p> <pre><font color="#333333">x = ['ab', 'cd']<br></font><font color="#333333">for i in x:<br></font><font color="#333333">&nbsp; &nbsp; i.upper()&nbsp;<br></font><font color="#333333">print(x)</font></pre>

  1. [‘ab’, ‘cd’]
  2. [‘AB’, ‘CD’]
  3. [None, None]
  4. none of the mentioned
Qus. 2 : <p>What will be the output of the following Python code snippet?</p><pre>for i in [1, 2, 3, 4][::-1]:<br>&nbsp; &nbsp; print (i)</pre><div><br></div>

  1. 1 2 3 4
  2. 4 3 2 1
  3. error
  4. none of the mentioned
Qus. 3 : <p>What will be the output of the following Python code snippet?</p><pre><span style="font-size: 14px;">a = [0, 1, 2, 3]<br></span><span style="font-size: 14px;">for a[-1] in a:<br></span><span style="font-size: 14px;">&nbsp; &nbsp; print(a[-1])</span></pre>

  1. 0 1 2 3
  2. 0 1 2 2
  3. 3 3 3 3
  4. error
Qus. 4 : Which of the following commands will create a list?

  1. list1 = list()
  2. list1 = []
  3. list1 = list([1, 2, 3])
  4. all of the mentioned
Qus. 5 : What is the output when we execute list(“hello”)?

  1. [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
  2. [‘hello’]
  3. [‘llo’]
  4. [‘olleh’]
Qus. 6 : Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?

  1. 5
  2. 4
  3. None
  4. Error
Qus. 7 : Suppose list1 is [2445,133,12454,123], what is max(list1)?

  1. 2445
  2. 133
  3. 12454
  4. 123
Qus. 8 : Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?

  1. 3
  2. 5
  3. 25
  4. 1
Qus. 9 : Suppose list1 is [1, 5, 9], what is sum(list1)?

  1. 1
  2. 9
  3. 15
  4. Error
Qus. 10 : To shuffle the list(say list1) what function do we use?

  1. list1.shuffle()
  2. shuffle(list1)
  3. random.shuffle(list1)
  4. random.shuffleList(list1)
Qus. 11 : Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing operation?

  1. print(list1[0])
  2. print(list1[:2])
  3. print(list1[:-2])
  4. all of the mentioned
Qus. 12 : Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?

  1. Error
  2. None
  3. 25
  4. 2
Qus. 13 : <p><span style="font-size: 14px;">What is the output of the following code?</span><br></p><pre>list1=[2, 33, 222, 14, 25]<br>print(list1[ : -1 ])</pre>

  1. [2, 33, 222, 14]
  2. Error
  3. 25
  4. [25, 14, 222, 33, 2]
Qus. 14 : <p>What will be the output of the following Python code?</p><pre>names = ['Amir', 'Bear', 'Charlton', 'Daman']<br>print(names[-1][-1])</pre>

  1. A
  2. Daman
  3. Error
  4. n
Qus. 15 : Suppose list1 is [1, 3, 2], What is list1 * 2?

  1. [2, 6, 4]
  2. [1, 3, 2, 1, 3]
  3. [1, 3, 2, 1, 3, 2]
  4. [1, 3, 2, 3, 2, 1]
Qus. 16 : Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:

  1. [0, 1, 2, 3]
  2. [0, 1, 2, 3, 4]
  3. [0.0, 0.5, 1.0, 1.5]
  4. [0.0, 0.5, 1.0, 1.5, 2.0]
Qus. 17 : <p>What will be the output of the following Python code?</p><pre>list1 = [11, 2, 23]<br>list2 = [11, 2, 2]<br>print(list1 &lt; list2)</pre>

  1. True
  2. False
  3. Error
  4. None
Qus. 18 : To add a new element to a list we use which command?

  1. list1.add(5)
  2. list1.append(5)
  3. list1.addLast(5)
  4. list1.addEnd(5)
Qus. 19 : To insert 5 to the third position in list1, we use which command?

  1. list1.insert(3, 5)
  2. list1.insert(2, 5)
  3. list1.add(3, 5)
  4. list1.append(3, 5)
Qus. 20 : To remove string “hello” from list1, we use which command?

  1. list1.remove(“hello”)
  2. list1.remove(hello)
  3. list1.removeAll(“hello”)
  4. list1.removeOne(“hello”)
Qus. 21 : Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?

  1. 0
  2. 1
  3. 4
  4. 2
Qus. 22 : Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?

  1. 0
  2. 4
  3. 1
  4. 2
Qus. 23 : Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?

  1. [3, 4, 5, 20, 5, 25, 1, 3]
  2. [1, 3, 3, 4, 5, 5, 20, 25]
  3. [25, 20, 5, 5, 4, 3, 3, 1]
  4. [3, 1, 25, 5, 20, 5, 4, 3]
Qus. 24 : Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?

  1. [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
  2. [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
  3. [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
  4. [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
Qus. 25 : Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?

  1. [3, 4, 5, 20, 5, 25, 1, 3]
  2. [1, 3, 3, 4, 5, 5, 20, 25]
  3. [3, 5, 20, 5, 25, 1, 3]
  4. [1, 3, 4, 5, 20, 5, 25]
Qus. 26 : Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?

  1. [3, 4, 5, 20, 5, 25, 1]
  2. [1, 3, 3, 4, 5, 5, 20, 25]
  3. [3, 5, 20, 5, 25, 1, 3]
  4. [1, 3, 4, 5, 20, 5, 25]
Qus. 27 : <p>What will be the output of the following Python code?</p><pre><span style="font-size: 14px;">mylist=list("a#b#c#d".split('#'))<br></span><span style="font-size: 14px;">print(mylist)</span></pre><div><br></div>

  1. [‘a’, ‘b’, ‘c’, ‘d’]
  2. [‘a b c d’]
  3. [‘a#b#c#d’]
  4. [‘abcd’]
Qus. 28 : <p>What will be the output of the following Python code?</p><pre>myList = [1, 5, 5, 5, 5, 1]<br>max = myList[0]<br>indexOfMax = 0<br>for i in range(1, len(myList)):<br>&nbsp; &nbsp; if myList[i] &gt; max:<br>&nbsp; &nbsp; &nbsp; &nbsp; max = myList[i]<br>&nbsp; &nbsp; &nbsp; &nbsp; indexOfMax = i&nbsp; &nbsp;<br>print(indexOfMax)</pre>

  1. 1
  2. 2
  3. 3
  4. 4
Qus. 29 : <p>What will be the output of the following Python code?</p><pre>myList = [1, 2, 3, 4, 5, 6]<br>for i in range(1, 6):<br>&nbsp; &nbsp; myList[i - 1] = myList[i]&nbsp; &nbsp; &nbsp;<br>for i in range(0, 6):&nbsp;<br>&nbsp; &nbsp; print(myList[i], end = " ")</pre>

  1. 2 3 4 5 6 1
  2. 6 1 2 3 4 5
  3. 2 3 4 5 6 6
  4. 1 1 2 3 4 5
Qus. 30 : <p>What will be the output of the following Python code?</p><pre>list1 = [1, 3]<br>list2 = list1<br>list1[0] = 4<br>print(list2)</pre><div><br></div>

  1. [1, 3]
  2. [4, 3]
  3. [1, 4]
  4. [1, 3, 4]
Qus. 31 : <p>What will be the output of the following Python code?</p><pre>def f(values):<br>&nbsp; &nbsp; values[0] = 44<br>&nbsp; &nbsp;<br>v = [1, 2, 3]<br>f(v)<br>print(v)</pre>

  1. [1, 44]
  2. [1, 2, 3, 44]
  3. [44, 2, 3]
  4. [1, 2, 3]
Qus. 32 : What is “Hello”.replace(“l”, “e”)?

  1. Heeeo
  2. Heelo
  3. Heleo
  4. None
Qus. 33 : To retrieve the character at index 3 from string s=”Hello” what command do we execute (multiple answers allowed)?

  1. s[]
  2. s.getitem(3)
  3. s.__getitem__(3)
  4. s.getItem(3)
Qus. 34 : To return the length of string s what command do we execute?

  1. s.__len__()
  2. len(s)
  3. size(s)
  4. s.size()
Qus. 35 : Suppose i is 5 and j is 4, i + j is same as ________

  1. i.__add(j)
  2. i.__add__(j)
  3. i.__Add(j)
  4. i.__ADD(j)
Qus. 36 : What function do you use to read a string?

  1. input(“Enter a string”)
  2. eval(input(“Enter a string”))
  3. enter(“Enter a string”)
  4. eval(enter(“Enter a string”))
Qus. 37 : <p>What will be the output of the following Python code?</p><pre>values = [[3, 4, 5, 1], [33, 6, 1, 2]]<br>v = values[0][0]<br>for row in range(0, len(values)):<br>&nbsp; &nbsp; for column in range(0, len(values[row])):<br>&nbsp; &nbsp; &nbsp; &nbsp; if v &lt; values[row][column]:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = values[row][column]<br>print(v)</pre>

  1. 3
  2. 5
  3. 6
  4. 33
Qus. 38 : <p>What will be the output of the following Python code?</p><pre>values = [[3, 4, 5, 1], [33, 6, 1, 2]]<br>v = values[0][0]<br>for lst in values:<br>&nbsp; &nbsp; for element in lst:<br>&nbsp; &nbsp; &nbsp; &nbsp; if v &gt; element:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = element<br>print(v)</pre><div><br></div>

  1. 1
  2. 3
  3. 5
  4. 6
Qus. 39 : <p>What will be the output of the following Python code?</p><pre>values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]<br>for row in values:<br>&nbsp; &nbsp; row.sort()<br>&nbsp; &nbsp; for element in row:<br>&nbsp; &nbsp; &nbsp; &nbsp; print(element, end = " ")<br>&nbsp; &nbsp; print()</pre>

  1. The program prints two rows 3 4 5 1 followed by 33 6 1 2
  2. The program prints on row 3 4 5 1 33 6 1 2
  3. The program prints two rows 3 4 5 1 followed by 33 6 1 2
  4. The program prints two rows 1 3 4 5 followed by 1 2 6 33
Qus. 40 : <p>What will be the output of the following Python code?</p><pre>matrix = [[1, 2, 3, 4],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[4, 5, 6, 7],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[8, 9, 10, 11],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[12, 13, 14, 15]]&nbsp;&nbsp;<br>for i in range(0, 4):<br>&nbsp; &nbsp; &nbsp; print(matrix[i][1], end = " ")</pre>

  1. 1 2 3 4
  2. 4 5 6 7
  3. 1 3 8 12
  4. 2 5 9 13
Qus. 41 : <p>What will be the output of the following Python code?</p><pre>def m(list):<br>&nbsp; &nbsp; v = list[0]<br>&nbsp; &nbsp; for e in list:<br>&nbsp; &nbsp; &nbsp; &nbsp; if v &lt; e:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = e<br>&nbsp; &nbsp; return v<br>&nbsp; &nbsp;<br>values = [[3, 4, 5, 1], [33, 6, 1, 2]]&nbsp; &nbsp;<br>for row in values:&nbsp;<br>&nbsp; &nbsp; &nbsp; print(m(row), end = " ")</pre>

  1. 3 33
  2. 1 1
  3. 5 6
  4. 5 33
Qus. 42 : <p>What will be the output of the following Python code?</p><pre>data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]<br>def ttt(m):<br>&nbsp; &nbsp; v = m[0][0]<br>&nbsp; &nbsp; for row in m:<br>&nbsp; &nbsp; &nbsp; &nbsp; for element in row:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if v &lt; element: v = element&nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; return v<br>print(ttt(data[0]))</pre>

  1. 1
  2. 2
  3. 4
  4. 5
Qus. 43 : <p>What will be the output of the following Python code?</p><pre>points = [[1, 2], [3, 1.5], [0.5, 0.5]]<br>points.sort()<br>print(points)</pre>

  1. [[1, 2], [3, 1.5], [0.5, 0.5]]
  2. [[3, 1.5], [1, 2], [0.5, 0.5]]
  3. [[0.5, 0.5], [1, 2], [3, 1.5]]
  4. [[0.5, 0.5], [3, 1.5], [1, 2]]
Qus. 44 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""><span style="font-size: 12.6px;">a=[10,23,56,[78]] b=list(a) a[3][0]=95 a[1]=34 print(b)</span><br></pre>

  1. [10,34,56,[95]]
  2. [10,23,56,[78]]
  3. [10,23,56,[95]]
  4. [10,34,56,[78]]
Qus. 45 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""><span style="font-size: 12.6px;">lst=[3,4,6,1,2] lst[1:2]=[7,8] print(lst)</span><br></pre>

  1. [3, 7, 8, 6, 1, 2]
  2. Syntax error
  3. [3,[7,8],6,1,2]
  4. [3,4,6,7,8]
Qus. 46 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""> <span style="font-size: 12.6px;">a=[1,2,3] b=a.append(4) print(a) print(b)</span></pre>

  1. [1,2,3,4] [1,2,3,4]
  2. [1, 2, 3, 4] None
  3. Syntax error
  4. [1,2,3] [1,2,3,4]
Qus. 47 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""><span style="font-size: 12.6px;">a=[13,56,17] a.append([87]) a.extend([45,67]) print(a)</span><br></pre><p style="border-radius: 0px; padding: 0px;"><br></p>

  1. [13, 56, 17, [87], 45, 67]
  2. [13, 56, 17, 87, 45, 67]
  3. [13, 56, 17, 87,[ 45, 67]]
  4. [13, 56, 17, [87], [45, 67]]
Qus. 48 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""> <span style="font-size: 12.6px;">word1="Apple" word2="Apple" list1=[1,2,3] list2=[1,2,3] print(word1 is word2) print(list1 is list2)</span></pre><div><br></div>

  1. True True
  2. False True
  3. False False
  4. True False
Qus. 49 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""><span style="font-size: 12.6px;">places = ['Bangalore', 'Mumbai', 'Delhi'] places1 = places places2 = places[:] places1[1]="Pune" places2[2]="Hyderabad" print(places)</span><br></pre>

  1. [‘Bangalore’, ‘Pune’, ‘Hyderabad’]
  2. [‘Bangalore’, ‘Pune’, ‘Delhi’]
  3. [‘Bangalore’, ‘Mumbai’, ‘Delhi’]
  4. [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’]
Qus. 50 : <p>What will be the output of the following Python code?</p><pre><span style="font-size: 14px;">a= [1, 2, 3, 4, 5]<br></span><span style="font-size: 14px;">for i in range(1, 5):<br></span><span style="font-size: 14px;">&nbsp; &nbsp; a[i-1] = a[i]<br></span><span style="font-size: 14px;">for i in range(0, 5):<br></span><span style="font-size: 14px;">&nbsp; &nbsp; print(a[i],end = " ")</span></pre><div><br></div>

  1. 5 5 1 2 3
  2. 5 1 2 3 4
  3. 2 3 4 5 1
  4. 2 3 4 5 5
Qus. 51 : <p>What will be the output of the following Python code?</p><pre><span style="font-size: 14px;">a=["Apple","Ball","Cobra"]<br></span><span style="font-size: 14px;">a.sort(key=len)<br></span><span style="font-size: 14px;">print(a)</span></pre><div><br></div>

  1. [‘Apple’, ‘Ball’, ‘Cobra’]
  2. [‘Ball’, ‘Apple’, ‘Cobra’]
  3. [‘Cobra’, ‘Apple’, ‘Ball’]
  4. Invalid syntax for sort()
Qus. 52 : Suppose a list with name arr, contains 5 elements. You can get the 2nd element from the list using :

  1. arr[-2]
  2. arr[2]
  3. arr[-1]
  4. arr[1]
Qus. 53 : <p>What will be the output of the following ?</p><pre><span style="font-size: 14px;">print((range(4)))</span></pre>

  1. 0,1,2,3
  2. [0,1,2,3]
  3. range(0, 4)
  4. (0,1,2,3)
Qus. 54 : How can we create an empty list in Python ?

  1. list=()
  2. list.null
  3. null.list
  4. list=[]
Qus. 55 : Assume q= [3, 4, 5, 20, 5, 25, 1, 3], then what will be the items of q list after q.pop(1) ?

  1. [3, 4, 5, 20, 5, 25, 1, 3]
  2. [1, 3, 3, 4, 5, 5, 20, 25]
  3. [3, 5, 20, 5, 25, 1, 3]
  4. [1, 3, 4, 5, 20, 5, 25]
Qus. 56 : <p>what is the output of the following code?</p><pre><span style="font-size: 12.6px;">M=['b' *x for x in range(4)] print(M)</span><br></pre>

  1. ['', 'b', 'bb', 'bbb']
  2. ['b,'bb', "bbb', "bbbb']
  3. ['b','bb', bbb']
  4. none of these
Qus. 57 : <p>What will be the output of the following Python code?</p><pre>list1=[1,3]<br>list2=list1<br>list1[0]=4<br>print(list2)</pre><div><br></div>

  1. [1, 4]
  2. [1,3, 4]
  3. [4,3]
  4. [1,3]
Qus. 58 : Which function is used to add an element (5) in the list1?

  1. list1.sum(5)
  2. list1.add(5)
  3. listl.append(5)
  4. list1.addelement(5)
Qus. 59 : To add a new element to a list we use which Python command?

  1. list1.addEnd (5)
  2. list1.addLast (5)
  3. list1.append (5)
  4. list1.add(5)
Qus. 60 : Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is the value of list1.count(5)?

  1. 0
  2. 4
  3. 1
  4. 2
Qus. 61 : To remove string "hello" from list1, we use which command ?

  1. list1.remove("hello")
  2. list1.remove(hello)
  3. list1.removeAll("hello")
  4. list1.removeOne ("hello")
Qus. 62 : What is the output when we execute list("hello")?

  1. ['llo']
  2. ['hello']
  3. ['h', 'e', T', '1', 'o']
  4. None of the above
Qus. 63 : Suppose a list with name arr, contains 5 elements. You can get the 2nd element from the list using:

  1. arr[-2]
  2. arr[2]
  3. arr[-1]
  4. arr[1]
Qus. 64 : which function is used to remove last item from a list?

  1. pop()
  2. delete()
  3. remove_last()
  4. delete_last()
Qus. 65 : <p>What will be the output of the following code?</p><pre><span style="font-size: 14px;">list1=[2,33,222,14,25]<br></span><span style="font-size: 14px;">print(list1[-1])</span></pre>

  1. 2
  2. 33
  3. 14
  4. 25
Qus. 66 : Which of the following is the correct way to check if a value exists in a list?

  1. list.contains(value)
  2. list.index(value)
  3. list.contains(value)
  4. value in list
Qus. 67 : <p>What will be the output of the following code snippet?</p><pre><span style="font-size: 14px;">a=[1,2,3,4,5,6,7,8,9]<br></span><span style="font-size: 14px;">a[::2]=10,20,30,40,50,60<br></span><span style="font-size: 14px;">print(a)</span></pre><div><br></div>

  1. ValueError: attempt to assign sequence of size 6 to extended slice of size 5
  2. [10, 2, 20, 4, 30, 6, 40, 8, 50, 60]
  3. [1, 2, 10, 20, 30, 40, 50, 60]
  4. [1, 10, 3, 20, 5, 30, 7, 40, 9, 50, 60]
Qus. 68 : <p>What is the correct command to shuffle the following list?</p><pre><span style="font-size: 14px;">fruit=['apple', 'banana', 'papaya', 'cherry']</span></pre><div><br></div><p> </p>

  1. fruit.shuffle()
  2. shuffle(fruit)
  3. random.shuffle(fruit)
  4. random.shuffleList(fruit)
Qus. 69 : <p>What is the output of the following code:</p><pre><span style="font-size: 14px;">L=['a','b','c','d']<br></span><span style="font-size: 14px;">print ( "".join(L))</span></pre><div><br></div>

  1. Error
  2. None
  3. abcd
  4. [‘a’,’b’,’c’,’d’]
Qus. 70 : Which statement is correct??

  1. . List is mutable & Tuple is immutable
  2. List is immutable & Tuple is mutable
  3. Both are mutable
  4. Both are immutable

Programs

python program to find the 2nd largest number from the list of the numbers entered through keyboard.

View Solution


python program that creates a list of numbers from 1 to 20 that are divisible by 4

View Solution


python program to define a list of countries that are a member of BRICS Check whether a county is member of BRICS or not

View Solution


Python program to read marks of six subjects and to print the marks scored in each subject and show the total marks

View Solution


Python program to read prices of 5 items in a list and then display sum of all the prices product of all the prices and find the average

View Solution


Python program to count the number of employees earning more than 1 lakh per annum

View Solution


python program to create a list of numbers in the range 1 to 10 Then delete all the even numbers from the list and print the final list

View Solution


python program to generate in the Fibonacci series and store it in a list Then find the sum of all values

View Solution


python program to swap two values using tuple assignment

View Solution


python program using a function that returns the area and circumference of a circle whose radius is passed as an argument

View Solution


python program that has a list of positive and negative numbers Create a new tuple that has only positive numbers from the list

View Solution


Python Program that generate a set of prime numbers and another set of even numbers

View Solution


python program to find minimum element from a list of elements along with its index in the list

View Solution


Python program to calculate mean of a given list of numbers

View Solution


Python program to search for an element in a given list of numbers

View Solution


python program to count frequency of a given element in a list of numbers

View Solution


python program to calculate the sum of integers of the list

View Solution


python program to reverses an array of integers

View Solution


python program to creates a third list after adding two lists

View Solution


python program to store the product information in dictionary

View Solution


python program to create a dictionary whose keys are month names and values are their corresponding number of days

View Solution


python program that input a list, replace it twice and then prints the sorted list in ascending and descending order

View Solution


python program to check if the maximum element of the list lies in the first half of the list or in the second half

View Solution


python program to display the maximum elements from the two list along with its index in its list

View Solution


python program to swap list elements with even and locations

View Solution


python program to rotate list elements with next elements

View Solution


python program to find maximum and minimum values in tuple

View Solution


python program to interchange the values of two tuple

View Solution


python program to interchange the values of two tuple

View Solution


python program to increment the elements of list

View Solution


python program to enter a list containing number between 1 and 12 then replace all the entries in the list that are greater than 10 with 10

View Solution


python program that reverse the list of integers in place

View Solution


python program to input two list and create third list which contains all elements of the first elements followed by all elements of second list

View Solution


python program to inter list of string and create a new list that consist of those strings with their first characters removed

View Solution


python program to create a list using for loop which consist of integers 0 through 49

View Solution


python program to create a list using for loop which consist square of the integers 1 through 50

View Solution


python program to create a list using for loop which consist a bb ccc dddd that ends with 26 copies of the letter z.

View Solution


python program that takes any two list L and M of the same size and adds their elements together in another third list

View Solution


python program to rotates the elements of list so that fist elements move to second and second to the third

View Solution


python program to move all duplicate element to the end of list

View Solution


python program to display the maximum and minimum values form specified range of indexes of a list

View Solution


python program to compare two equal sized list and print the first index where they differ

View Solution


python program to enter names of employees and their salaries as input and store them in a dictionary

View Solution


python program to count the number of times a character appears in a given string

View Solution


python program to convert a number entered by the user into its corresponding number in words

View Solution


python program Repeatedly ask the user to enter a team name

View Solution


python program that repeatedly asks the user to enter product names and prices

View Solution


Python Program to Create a dictionary whose keys are month name and whose values are number of days in the corresponding month

View Solution


python program to store the detail of 10 students in a dictionary at the same time

View Solution


python program to Create a dictionary with the opposite mapping

View Solution


python program that lists the over lapping keys of the two dictionaries if a key of d1 is also a key of d2 the list it

View Solution


python program that checks if two same values in a dictionary have different keys

View Solution


python program to check if a dictionary is contained in another dictionary

View Solution


python program to create a new dictionary D2 having same keys as D1 but values as the sum of the list elements

View Solution


python program to create a dictionary has three keys assets liabilities and capital

View Solution


python program that create a tuple storing first 9 terms of Fibonacci series

View Solution


python program that receives the index and return the corresponding value

View Solution


Python program that receives a Fibonacci series term and returns a number telling which term it is

View Solution


python program to create a nested tuple to store roll number name and marks of students

View Solution


python program that interactively creates a nested tuple to store the marks in three subjects for five students

View Solution


Python Program to accept three distinct digits and print all possible combinations from the digits

View Solution


Python Program to find the successor and predecessor of the largest element in list

View Solution


Python function that takes a string as parameter and returns a string with every successive repetitive character replaced

View Solution


Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad