-2

If I have the following code

if "ERB-776-RAD" in my_data.any():
    print("I found it!")

Where the my_data is something as follows:

my_data = ['ERA-776-TRF','FDS-342-FHS','EBR-776-RAD'...]

How would I print the actual value I am looking for (ERB-776-RAD) instead of printing "I found it"?

1 Answers1

2

Use the in statement to test if the list contains the string. Then you can print anything you want if it's True. You can use string formatting to insert the value dynamically (in the code below, I am using f-strings).

my_data = ['ERA-776-TRF','FDS-342-FHS','EBR-776-RAD']
target = "ERB-776-RAD"
if target in my_data:
    print(f"I found {target}")

As @chepner commented, lists do not have an any method. (If you saw .any(), it might have been with a numpy array or pandas dataframe.)

jkr
  • 17,119
  • 2
  • 42
  • 68