List Examples

Program 

#Example:1-removing duplicates in list

list1=[1,2,2,3,55,98,65,65,13,29] # Declare an empty list that will store unique values
print("list is:",list1)
list2=[]  
for i in list1: 
    if i not in list2:  
        list2.append(i)  
print("after remove duplicates:",list2)
print("=======================================")


#Example:2- Write a program to find the sum of the element in the list.

list1 = [3,4,5,9,10,12,24]
print("list is:",list1)
sum = 0  
for i in list1:  
    sum = sum+i      
print("The sum is:",sum)
print("=======================================")


#Example: 3- Write the program to find the lists consist of at least one common element.

list1 = [1,2,3,4,5,6]  
list2 = [7,8,9,2,10]  
for x in list1:  
    for y in list2:  
        if x == y:  
            print("The common element is:",x)
print("=======================================")



OUTPUT:


list is: [1, 2, 2, 3, 55, 98, 65, 65, 13, 29]
after remove duplicates: [1, 2, 3, 55, 98, 65, 13, 29]
=======================================
list is: [3, 4, 5, 9, 10, 12, 24]
The sum is: 67
=======================================
The common element is: 2
=======================================