How to Check if Element Exists in List in Python

In this tutorial, we will program 'How to Check if an Element Exists in a List in Python'. We will learn how to identify if a specific element is present in a list. The objective is to enable you to efficiently check for the existence of elements within a list. I will provide a sample program to demonstrate the actual coding of this tutorial.

This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics of programming for finding a missing element in a list. I will do my best to provide you with the easiest way for checking the list. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. lst=[ 1, 6, 3, 5, 3, 4 ]
  2.  
  3.  
  4. ret = False
  5.  
  6. while True:
  7. print("\n================== Check if Element Exists in List ==================\n\n")
  8.  
  9.  
  10. i=int(input("Enter a number: "))
  11.  
  12. if i in lst:
  13. print("The number you entered is already exist!")
  14. else:
  15. print("The number you entered is currently not exist!")
  16.  
  17.  
  18. opt = input("\nDo you want to try again?(yes/no): ")
  19.  
  20. if opt.lower() == 'yes':
  21. ret=False
  22. elif opt.lower() == 'no':
  23. ret=True
  24. print("Exiting program....")
  25.  
  26. else:
  27. print("Please enter yes/no:")
  28. break
  29.  
  30. if ret == False:
  31. continue

This Python script checks if a user-provided number exists in a predefined list. It repeatedly prompts the user to enter a number and then checks if that number is present in the list lst. It prints a message indicating whether the number exists in the list. After each check, it asks the user if they want to try again or exit the program.

Output:

The How to Check if Element Exists in List in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Check if Element Exists in List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment