Q-1.Write a Python program to remove duplicates from a List.
Input: [10,20,20,20,30,50]
Output: 20 is duplicate 20 is duplicate [10, 20, 30, 50]

Python Code:

#remove the duplicate elements from list

def remove_duplicate(num):
    
    new=[]
    for ele in list1:
        if not ele in new:
            new.append(ele)
        else:
            print(ele,"is duplicate")
    return new

list1=[10,20,20,20,30,50]
print(remove_duplicate(list1))
Q-2. Write a Python program to check a List is empty or not.
Input:[] 
Output:list is empty

Python Code:

#to check list is empty or not

l=[]
if  not l:
    print("list is empty")
else:
    print("list is not empty")
Q-3.Write a Python program to get the difference between the two Lists.
Input:list_1=[10,20,30,40,50]
            list_2=[20,40,60]
Output:20 found in list_2 40 found in list_2 [10, 30, 50]

Python Code:

#find the difference between the two list

def list_diff(list1,list2):
    new_list=[]
    for ele in list1:
        if not ele in list2:
            new_list.append(ele)
        else:
            print(ele,"found in list_2")
    return new_list

list_1=[10,20,30,40,50]
list_2=[20,40,60]

print(list_diff(list_1,list_2))
Q-4.Write a Python program to multiplies all the items in a List. 
Input:[1,2,-4]
Output: -8(multiply of items in list)

Python Code:

#multiply items of list

def mul_item(num_list):
    mul=1
    for ele in num_list:
        mul*=ele
    return mul
print(mul_item([1,2,-4]))
        
Q-5.Write a Python program to convert a list of characters into a string.
Input:['p','y','t','h','o','n']
Output: python

Python Code:

#convert a  list into string

lst=['p','y','t','h','o','n']
new_lst=''.join(lst)
print(new_lst)
        


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert