1. Write a Python program to check whether a list contains a sublist. input: a=[2,4,3,5,7] b=[4,3] c=[3,7] output: is_sublist(a,b) is_sublist(a,c)
# Answer 1
def is_sublist(list,sublist):
  if len(sublist) == 0:
    return True
  if len(list) == 0:
    return False
  if list[:len(sublist)] == sublist:
    return True
  return is_sublist(list[1:],sublist)
a = [2,4,3,5,7]
b = [4,3]
c = [3,7]

print("Sublist is ",is_sublist(a,b))
print("Sublist is ",is_sublist(a,c))
Sublist is  True
Sublist is  False
  1. Write a Python program to find common items from two lists. input: color1=["Red","Green","Orange","White"] color2=["Black","Green","White","Pink"] output: {"Green","White"}
# Answer 2
color1=["Red","Green","Orange","White"]
color2=["Black","Green","White","Pink"]
set1 = set(color1)
set2 = set(color2)
common_items = set1.intersection(set2)
print(set1)
print(set2)
print("Common items in the list are",common_items)
{'Green', 'Orange', 'White', 'Red'}
{'Green', 'Black', 'White', 'Pink'}
Common items in the list are {'Green', 'White'}
  1. Write a Python program to get the difference between the two lists Input: list1 = [1, 2, 3, 4] list2 = [1, 2] Output: [3,4]
# Answer 3
list1=[1,2,3,4]
list2=[1,2]
difference = [x for x in list1 if x not in list2]
print("Difference between two lists is",difference)
Difference between two lists is [3, 4]
  1. Write a Python program to generate all permutations of a list in Python Input: [1,2,3] Output: [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
# Answer 4
import numpy as np
def generate_permutations(list):
    if len(list) == 0:
        return [[]]

    permutations = []
    for i in range(len(list)):
        rest = np.delete(list, i)
        for p in generate_permutations(rest):
            permutations.append([list[i]] + p)

    return permutations

input_list = [1, 2, 3]
permutations = generate_permutations(np.array(input_list))

print("The permutations of the",input_list," are - ",[list(p) for p in permutations])
The permutations of the [1, 2, 3]  are -  [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
  1. Write a Python program to remove duplicates from a list. Input: a = [10,20,30,20,10,50,60,40,80,50,40] Output : {40, 10, 80, 50, 20, 60, 30}
# Answer 5
a =[10,20,30,20,10,50,60,40,80,50,40]
remove_duplicate= set(a)
print("The elements after removing the duplicate are", remove_duplicate)
The elements after removing the duplicate are {40, 10, 80, 50, 20, 60, 30}