codescracker
python

Python Program to Search an Element in a List



« Previous Program Next Program »

This article deals with some programs in Python that searches an element in a list. Here both element and list must be entered by user at run-time. Here are the list of programs covered in this article:

Search an Element in a List

The question is, write a Python program to search a given element from a given list. The program given below is answer to this question:

mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
    val = int(input())
    mylist.append(val)

print("Enter an element to be search: ")
elem = int(input())

for i in range(5):
    if elem == mylist[i]:
        print("\nElement found at Index:", i)
        print("Element found at Position:", i+1)

Here is its sample run:

python search an element in list

Now supply the input say 10, 20, 30, 40, 50 as five elements and 10 as element to be search, then press ENTER key to search and print the index and position of the given element in the list like shown in the sample output given below:

search element in list python

Search an Element in a List of n Elements

After modifying previous program, I've created another program, that is the program given below. This program allows user to define the size of list along with its element and the element that is going to be search from the given list. Let's take a look at the program and its sample run given below:

mylist = list()

print("Enter the size of list: ", end="")
tot = int(input())

print("Enter", tot, "elements for the list: ", end="")
for i in range(tot):
    mylist.append(input())

print("\nEnter an element to be search: ", end="")
elem = input()

for i in range(tot):
    if elem == mylist[i]:
        print("\nElement found at Position:", i+1)

Here is its sample run with user input, 6 as size and 50, 51, 52, 53, 54, 55 as six elements, and 51 as element to be search:

search number in list python

Here is another sample run with user input 6 as size, p, y, t, h, o, n as six elements, and p as element to be search:

python search element in list program

Modified Version of Previous Program

This program is created to deal with conditions like, when element does not found, only one element found, duplicate element found.

mylist = list()

print("Enter the size of list: ", end="")
tot = int(input())

print("Enter", tot, "elements for the list: ", end="")
for i in range(tot):
    mylist.append(input())

print("\nEnter an element to be search: ", end="")
elem = input()

poslist = list()
for i in range(tot):
    if elem == mylist[i]:
        poslist.append(i+1)

if len(poslist) == 0:
    print("\nElement not found in the list!")
elif len(poslist) == 1:
    print("\nElement found at position:", poslist[0])
else:
    print("\nElement found at positions:", end="")
    for i in range(len(poslist)):
        print(poslist[i], end=" ")

Here is its sample run with user input, 6 as size and 1, 2, 3, 2, 4, 2 as six elements, and 2 as element to be search:

python search duplicate elements in list

Here is another sample run with user input, 4 as size, 10, 20, 40, 50 as four elements, and 30 as element to be search:

search all occurrence of element python

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker