Python Programming Questions - LIST

Python Programming Questions - Operation on List[SET-1]

 

1. Python program to check if element exists in list.

count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

element = int(input(" Enter The Elements To Be Checked In List :"))
for j in range(count):
    if element in myList:
        print(element," is Present in List at Index ", myList.index(element))
        break
else:
    print(element," is Not Present in List ")


Enter The Number of Elements Present In List : 5
20
60
80
85
65

Enter The Elements To Be Checked In List :85
85  is Present in List at Index  3


2. Different ways to clear a list in Python

 #Clearing List Using Method 1 #########################################
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

print(" ",myList)

#Method1
myList.clear()
print(" Method 1 \n ",myList)
#######################################################################
#Clearing List Using Method 2 #########################################
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

print(" ",myList)
#Method2
myList = []
print(" Method 2 \n ",myList)
#######################################################################
#Clearing List Using Method 3 #########################################
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

print(" ",myList)
#Method3
myList *= 0
print(" Method 3 \n ",myList)
#######################################################################
#Clearing List Using Method 4 #########################################
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

print(" ",myList)
#Method4
del myList[:]
print(" Method 4 \n ",myList)
#######################################################################


Enter The Number of Elements Present In List : 5
12
24
63
45
87
 [12, 24, 63, 45, 87]
 Method 1
  []
 Enter The Number of Elements Present In List : 5
10
210
30
40
50
  [10, 210, 30, 40, 50]
 Method 2
  []
 Enter The Number of Elements Present In List : 2
20
30
  [20, 30]
 Method 3
  []

Enter The Number of Elements Present In List : 4
20
40
60
80
  [20, 40, 60, 80]
 Method 4
  []


3. Python Program Reversing a List 

count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

#Method1
revList = myList[::-1]
print(" Original List : ",myList)
print(" Reversed List : ",revList)


count = int(input("\n\n Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

#Method2
print(" Original List : ",myList)
myList.reverse()
print(" Reversed List : ",myList)

 
Enter The Number of Elements Present In List : 5
10
20
45
65
85
 Original List :  [10, 20, 45, 65, 85]
 Reversed List :  [85, 65, 45, 20, 10]


 Enter The Number of Elements Present In List : 5
10
40
60
80
85
 Original List :  [10, 40, 60, 80, 85]
 Reversed List :  [85, 80, 60, 40, 10]




4. Python program to Cloning or Copying a list

count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

clone_List =[]
clone_List.extend(myList)

copyList = []
copyList = myList.copy()

print(" Original List : ",myList)
print(" Clonned List : ",clone_List)
print(" Clonned List : ",copyList)


Enter The Number of Elements Present In List : 5
55
65
85
80
84
 Original List :  [55, 65, 85, 80, 84]
 Clonned List :  [55, 65, 85, 80, 84]
 Clonned List :  [55, 65, 85, 80, 84]



5. Python Program To Count occurrences of an element in a list

count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
    myList.append(int(input()))

element = int(input(" Enter The Elements Whose Occurrence Has To Be Counted In List : "))
occur = myList.count(element)
print(element ," has been occurred ",occur, " times in given List ",myList)



element1 = int(input(" Enter The Elements Whose Occurrence Has To Be Counted In List : "))
counter = 0
for j in range(0,len(myList)):
    if myList[j] == element1:
        counter = counter + 1
print(element1 ," has been occurred ",counter," times in given List ",myList)
    


Enter The Number of Elements Present In List : 5
10
60
85
85
85
 Enter The Elements Whose Occurrence Has To Be Counted In List : 85
85  has been occurred  3  times in given List  [10, 60, 85, 85, 85]
 Enter The Elements Whose Occurrence Has To Be Counted In List : 10
10  has been occurred  1  times in given List  [10, 60, 85, 85, 85]
 


Comments

  1. Thanks for your valuable feedback. This feedback will help in improving quality of service. Please do share and follow to get more regularly.

    ReplyDelete

Post a Comment