[PYTHON] Search by the value of the instance in the list

The following code is efficient, flexible, and concise when exploring target_list linearly. next(( {x} for {x} in {target_list} if {condition} ),None) The second variable is the value returned if not found. By using next, it is possible to fetch the first one among the conditions, which is efficient.

Example

class Hoge(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
hoge_list=[Hoge(10,20),Hoge(10,30),Hoge(5,3),...]
found=next( (hoge for hoge in hoge_list if hoge.x==10 and hoge.y==30) ,None)

reference http://dev.ionous.net/2009/01/python-find-item-in-list.html

Recommended Posts

Search by the value of the instance in the list
In search of the fastest FizzBuzz in Python
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
Find the divisor of the value entered in python
In search of the best random dot stereogram (RDS).
Get the image of "Suzu Hirose" by Google image search.
○○ Solving problems in the Department of Mathematics by optimization
[python] Get the list of classes defined in the module
[Golang] Specify an array in the value of map
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
List of nodes in diagrams
Methods available in the list
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Get the value of a specific key in a list from the dictionary type in the list with Python
I investigated the calculation time of "X in list" (linear search / binary search) and "X in set"
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
Get the number of occurrences for each element in the list
Create a new list by combining duplicate elements in the list
Find the diameter of the graph by breadth-first search (Python memory)
Extract the value of dict or list as a string
Notes on coloring by value in the matplotlib scatter plot
Google search for the last line of the file in Python
Get index of nth largest / smallest value in list in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
How to check if the contents of the dictionary are the same in Python by hash value
The story of participating in AtCoder
Judgment of if by list comprehension
About the return value of pthread_mutex_init ()
About the return value of the histogram.
List of packages installed by conda
The story of the "hole" in the file
The meaning of ".object" in Django
About the basics list of Python basics
[Python] Display only the elements of the list side by side [Vertical, horizontal]
[Understanding in the figure] Management of Python virtual environment by Pipenv
Use the vector learned by word2vec in the Embedding layer of LSTM
Read the standard output of a subprocess line by line in Python
Learning by ABC173C (bit full search, copying of multidimensional list, one-dimensionalization of multidimensional list)
[python] Get the rank of the values in List in ascending / descending order
[Understanding in 3 minutes] The beginning of Linux
The story of an error in PyOCR
[Linux] List of Linux commands used in practice
Display a list of alphabets in Python 3
Algorithm Gymnastics 24 Middle of the Linked List
Implement part of the process in C ++
Get the column list & data list of CASTable
Generate a list packed with the number of days in the current month.
OR the List in Python (zip function)
Change the list in a for statement
Python> sys.path> List of strings indicating the path to search for modules
[Linux] Command to get a list of commands executed in the past
I measured 6 methods to get the index of the maximum value (minimum value) of the list
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
I want to sort a list in the order of other lists
Read the file line by line in Python
Read the file line by line in Python