Python List Interview Coding Question Pratice With Solution

  • Last updated: August 12, 2024 By Sunil Shaw

Whether you’re preparing for a job interview or simply looking to sharpen your Python skills, this resource is designed to help you master list-related coding challenges commonly encountered in interviews. Throughout this practice exercise, you’ll find a series of Python coding questions focused on lists, along with detailed solutions to guide you through the problem-solving process. By working through these exercises, you’ll gain confidence in tackling list-related problems, enhance your problem-solving abilities, and solidify your understanding of Python programming concepts. Let’s dive in and start practicing!. Try Our python editor to solve the question. Editor are available in every questions tab. Don’t try to copy before try it by yourself.

Exercise 1: Concatenate two lists index-wise

Write an python program to add two lists index-wise. Create new list that contain 0th index item from the both list, then the 1st item and so on till the last element. Any left item will be added in the last of new list.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = ["M", "na", "i", "Ro"]
list2 = ["y", "me", "s", "nny"]
['My', 'name', 'is', 'Ronny']

If if both of the list have same length. Use loop and take one-one item from both of the list add the word together and to new list. Then return new list

def con(lst1, lst2):
    
    if len(lst1) == len(lst2):
        new_list = []
        iterate = len(lst1)
        for i in range(iterate):
            new_list.append(lst1[i]+lst2[i])
        return new_list
    else:
        return "Lists must have the same length"


list1 = ["M", "na", "i", "Ro"]
list2 = ["y", "me", "s", "nny"]

print(con(list1, list2))

Exercise 2: Reverse a list in Python

Write an Python programm to reverse all of list’s item. Do not try to copy the code, before do it by your own after check the solution.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = [10, 9, 8, 7, 6]
[6, 7, 8, 9, 10]

Loop over list, by list’s length times. Subtract length by the 1 and current loop number. Pass it to the indexing method and add the item to new list.

def reverse_a_list(lst):
    new_list = []
    length_of_list = len(lst)
    for i in range(length_of_list):
        new_list.append(lst[length_of_list-1-i])
    return new_list

list1 = [10, 9, 8, 7, 6]
print(reverse_a_list(list1))

Exercise 3: Concatenate two lists in the following order

Write an program to concatenate two lists in a order.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = ["Hello ", "GoodMorning "]
list2 = ["Ram", "Bhawna"]
['Hello Ram', 'Hello Bhawna', 'GoodMorning Ram', 'GoodMorning Bhawna']

Use For loop iterate over every item of first list and then second list. Take First Item of first list and first item of second list and add into a new list. Do it same for every item.

def concatinate_two_list(lst1, lst2):
    new_list = []
    for i in lst1:
        for j in lst2:
            new_list.append(i + j)
    return new_list




list1 = ["Hello ", "GoodMorning "]
list2 = ["Ram", "Bhawna"]

print(concatinate_two_list(list1, list2))

Exercise 4: Turn every item of a list into its square

Write an program to square of item of a given list.

  • Given :
  • Expected :
  • Hint :
  • Solution :
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 4, 9, 16, 25, 36, 49, 64, 81]

Write Your Own didn’t use built-in

def square_number(lst):
    new_list = []
    for i in lst:
        new_list.append(i*i)
    return new_list


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(square_number(numbers))

Exercise 5: Remove empty strings from the list of strings

Write an program to remove empty string.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = ["Mahi", "", "Bhawna", "Kabir", "", "Gandhi"]
['Mahi', 'Bhawna', 'Kabir', 'Gandhi']

Use For loop iterate one by one and check if string is not empty add into a new list.

def remove_empty(lst):
    new_list = []
    for i in lst:
        if i != "":
            new_list.append(i)
    return new_list

list1 = ["Mahi", "", "Bhawna", "Kabir", "", "Gandhi"]

print(remove_empty(list1))

Exercise 6: Iterate both lists simultaneously

Write an program to print items ony by one from both of the list in parallel.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = [1, 2, 3, 4]
list2 = [10, 20, 30, 40]
1 40
2 30
3 20
4 10

Check if length of both list is equal then print one-one item from both of the list.

def iterator(lst1, lst2):
    if len(lst1) == len(lst2):
        for i in range(len(lst1)):
            print(lst1[i], lst2[len(lst2)-i-1])

list1 = [1, 2, 3, 4]
list2 = [10, 20, 30, 40]

iterator(list1, list2)

Exercise 7: Extend nested list by adding the sublist

Write an program to add an list to another list at specific location.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = ["A", "B", ["C", ["D", "E", ["F", "G"], "K"], "L"], "M", "N"]

# sub list to add
sub_list = ["H", "I", "J"]
['A', 'B', ['C', ['D', 'E', ['F', 'G', 'H', 'I', 'J'], 'K'], 'L'], 'M', 'N']

Use indexing to add sublist

list1 = ["A", "B", ["C", ["D", "E", ["F", "G"], "K"], "L"], "M", "N"]

# sub list to add
sub_list = ["H", "I", "J"]


list1[2][1][2].extend(sub_list)

print(list1)

Exercise 8: Add new item to list after a specified item

Write an program to add item in a list at specified location.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
[10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]

Use Indexing to add new item in it.

list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]


list1[2][2].append(7000)
print(list1)

Exercise 9: Remove all occurrences from a list.

Write an program to remove all item that present more than one times. Remove the duplication.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = [5, 20, 15, 20, 25, 50, 20]
[5, 20, 15, 25, 50]

Loop over every item of an list check if the item is doesn’t exist in new list using if statement, then add the item in new list.

def remove_occurence(lst):
    new_list = []
    for i in lst:
        if i not in new_list:
            new_list.append(i)
    return new_list


list1 = [5, 20, 15, 20, 25, 50, 20]
print(remove_occurence(list1))

Exercise 10: Replace list’s item with new value if found

Write an program to replace item from the list.

  • Given :
  • Expected :
  • Hint :
  • Solution :
list1 = [10, 9, 8, 7, 6]
[6, 7, 8, 9, 10]

Loop over every item of list, use if state to check if old_number is not equal to item add in new list otherwise add new_number.

def replace(lst, old_number, new_number):
    new_list = []
    for i in lst:
        if i != old_number:
            new_list.append(i)
        else:
            new_list.append(new_number)
    return new_list
    
list1 = [10, 9, 8, 7, 6]
print(replace(list1, 7, 10))


Sunil Shaw

Sunil Shaw

About Author

I am a Web Developer, Love to write code and explain in brief. I Worked on several projects and completed in no time.

Related Articles

Popular Language Tutorials

If You want to build your own site Contact Us