Python Tutorial List - Find Element

Python Exercise Lists

Python lists provide index and count methods, both of which can be used to find elements.

Python Find Element - Index Method

The index () method is used to find the position (that is, the index) of an element in the list. If the element does not exist, it will cause a ValueError error, so it is best to use the count () method to judge it before searching.

The syntax of index () is:

listname.index (obj, start, end)

Among them, listname represents the name of the list, obj represents the element to find, start represents the starting position, and end represents the ending position.

The start and end parameters are used to specify the search range:

  • You can write neither start nor end, and the entire list will be retrieved at this time;
  • If you only write start but not end, it means to retrieve the elements from start to the end;
  • If both start and end are written, it means that the elements between start and end are retrieved.

The index method returns the index value in the list where the element is located.

Example of index method:

no = [32, 56, 79, 45, 2, 100, -3, -34.1, -888]
#Retrieve all elements in the list
print (no.index (2))
#Retrieve elements between 3 ~ 7
print (no.index (100, 3, 7))
#Retrieve elements after 4
print (no.index (2, 4))
#Retrieve a non-existing element
print (no.index (66))

The output is:

ValueError       Traceback (most recent call last)
[ipython-input-94-782b4504085f] in [module]
7 print (no.index (2, 4))
8 #Retrieve a non-existing element
----> 9 print (no.index (66))
ValueError: 66 is not in list

Python Find Element - Count Method

The count method is used to count the number of times an element appears in the list. The basic syntax is:

listname.count (obj)

Among them, listname represents the name of the list, and obj represents the elements to be counted.

If count returns 0, it means that the element does not exist in the list, so count method can also be used to determine whether an element in the list exists.

count method usage example:

nums = [23, 39, 67, 8, 39, 99, 7, -22.3, 39]
#Statistic element occurrences
print ("39 occured% d times"% nums.count (39))
#Determine if an element exists
if nums.count (99):
    print ("99 elements exist in the list")
    print ("99 element does not exist in the list")

The output is:

39 occured 3 times
99 elements exist in the list


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises