top of page
  • Writer's pictureAmmu Fredy

PYTHON :Interview Question based on the Topic List


Image: Photo by Chris Ried on Unsplash

Python has five Datatypes. The list is a built-in datatype used to store the collection of data. The list is the most versatile data type available in Python which can be written as a list of comma-separated values (items) between square brackets. The important thing about a list is that items in a list need not be of the same type.

•Default value is [], list()

Creating a list is as simple as putting different comma-separated values between square brackets.

list1 = ['Physics','Maths',2000,2004]

list2 = [1,2,3,4,5]

list3 = ['hello','world','learn','python']

Similar to string indices, list indices start at 0.


KEY POINTS FOR LIST

  • A list can be used for storing a group of elements enclosed within [ ]

  • List will allow Duplicate Elements

  • List will allow null values

  • List will maintain the insertion Order

  • List is mutable

  • We can retrieve the data from List based on Index

Method

Methods: Description

append() : Add a single element to the end of the list

clear() : Removes all Items from the List

copy() : Returns Shallow Copy of a List

count() : returns occurrences of element in a list

extend() : Add Elements of a List to Another List

index() : returns smallest index of element in list

insert() : Inserts Element to The List

len() : Returns Length of an Object

max() : returns the largest element

min() : returns smallest element

pop() : Removes element at the given index

remove() : Removes item from the list

reverse(): Reverses a List

sort() : sorts elements of a list

sum(): Add items of an Iterable


Now let's look at interview questions based on the topic list

1. Find the duplicate in a list and print that list

a=[1,1,1,2,2]

b=[]

For i in a:

If i not in b:

b.append(i)

print(b)

2. Find the sum of elements of a list

a=[1,2,3,3,3]

sum=0

for i in range(0,len(a)):

sum=sum+a[i]

print(sum)

3. Print the first non-recurring element in a list

a = [1, 1, 1, 2, 2]

k = min(a,key=a.count)

print (k)

4. How to reverse a list

a =[5, 2, 1, 6, 3]

a.sort()

print(a)

5.How to clear the list

a =[5, 2, 1, 6, 3]

a.clear()

print(a)

6.How to remove the last element of that list

a =[5, 2, 1, 6, 3]

a.pop()

print(a)

7. How to reverse a list without using the method and using the method

without method

a =[5, 2, 1, 6, 3]

print(a[::-1])

using method

a =[5, 2, 1, 6, 3]

a.reverse()

print(a)

8. How to insert another list in the middle of this list

a =[5, 2, 1, 6, 3]

b=[3,6]

q=[]

c=[]

d=[]

p=len(a)/2

q=a[0:int(p)+1]

c=a[int(p)+1:]

d=q+b+c

print(d)

9.How to another list using method

a =[5, 2, 1, 6, 3]

b=[3,6]

a.extend(b)

print(a)

10. Remove a element from the list

a =[5, 2, 1, 6, 3]

b=6

a.remove(b)

print(a)

11. check a particular element present in a list

a=[2,3,4,5,6,'a']

p=a.index('a')

print(p)

12. How to print a list of values with sentence

eg : dog is a pet

cat is a pet

mouse is a pet

a=['cat','dog','mouse']

for i in a:

print("%s is my no " %i)

13: How to iterate 2 list and make sentance

eg:The ammu's pet is cat The joann's pet is dog The emma's pet is mouse

a=['cat','dog','mouse']

b=['ammu','joann','emma']

z=zip(a,b)

for a,b, in z:

print("The %s's pet is %s " %(b,a))

14.How to remove duplicate from the list

a=['cat','dog','mouse','cat'

b=list(set(a))

print(b)

15. How to concat two list

a=[1,2,3,4]

b=[6,7,8]

c=a+b

print(c)

16. How to add an element to each element in a list

list = [1,2,3,4,5]

for index in range(len(list)): list[index] = list[index] +1

print(list)

or

list = [1,2,3,4,5]

p= [i+1 for i in list]

print(list)

17.To find the even numbers in a list

list = [1,2,3,4,5]

c=[]

for i in range(len(list)) :

if(list[i]%2==0):

c.append(list[i])

print(c)

18.How to iterate a list with a sublist and get the first element in each sublist

b = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]

for i in b:

print(i[0])

For this question,I will add some extra points ,if we want to get access the each element inside the sublist use temporary variables

b = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]

for i,j ,k in b:

print(i ,'is' ,j )

output is:

1 is 2 4 is 5 7 is 8 10 is 11 13 is 14

using temporary variables to access the elements

19. Subtract two lists

a=[1,2,3,4]

b=[4,5,6,7]

importNumPyy as np

a1=np.array(a)

b1=np.array(b)

print(b1-a1)

20. Reverse a list without using the function

a=[1,2,3,4]

print(a[::-1])


As I came across the topic List, these are questions I have tried to crack. Hope these questions are useful to you.


Comentários


bottom of page