11 Essential Python Interview Questions

Looking for Freelance Python Developer jobs? Design your lifestyle as a Python developer with Toptal.

Submit an Interview Question

1.) What will be the output of the code below? Explain your answer.

def extendList(val, list=[]):
   return list

list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')

print "list1 = %s" % list1
print "list2 = %s" % list2
print "list3 = %s" % list3

2.) Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are:
(a) even numbers, and
(b) from elements in the original list that had even indicesFor example, if list[2] contains a value that is even, that value should be included in the new list, since it is also at an even index (i.e., 2) in the original list. However, if list[3] contains an even number, that number should not be included in the new list since it is at an odd index (i.e., 3) in the original list.

A simple solution to this problem would be as follows

[x for x in list[::2] if x%2 == 0]

A simple solution to this problem would be as follows

#        0   1   2   3    4    5    6    7    8
list = [ 1 , 3 , 5 , 8 , 10 , 13 , 18 , 36 , 78 ]

the list comprehension [x for x in list[::2] if x%2 == 0] will evaluate to:

[10, 18, 78]

The expression works by first taking the numbers that are at the even indices, and then filtering out all the odd numbers.

Submit an interview question

Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Sign up now to see more profiles.

Hire Now