0

I'm supposed to make a function with a list and a title as a string, and then return the item from the list, based on the title.

def find_appointment(lst, title = ""):
    if title in lst:
        funn = lst.find(title)
        return funn
    else:
        print("No result")

appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22", "Funeral: 25.12.22"]
find_appointment(appointments, "Zoo")

I hoped to get "Zoo: 11.03.22", but instead got "No result"

The list here is just a random one I made up. In the actual list I won't know the positions of the items.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    `"Zoo"` _isn't_ in the list, `"Zoo" != "Zoo: 11.03.22"`. – jonrsharpe Oct 26 '22 at 16:23
  • 1
    Does this answer your question? [Finding a substring within a list in Python](https://stackoverflow.com/questions/13779526/finding-a-substring-within-a-list-in-python) – mkrieger1 Oct 26 '22 at 16:34
  • Ahh ok, thank you. I want it to be like a searchword to find the correct appointment. Do you maybe have an idea how I can do that? Thanks – HappilyStupid Oct 26 '22 at 16:35
  • See this : https://stackoverflow.com/questions/4843158/how-to-check-if-a-string-is-a-substring-of-items-in-a-list-of-strings (the link posted above will lead to it). – Swifty Oct 26 '22 at 16:37

3 Answers3

1

Here is my solution:

def find_appointment(lst, title = ""):
    for i in lst:
        if title in i:
            return index
        else:
            print("No result")

appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22", "Funeral: 25.12.22"]
print(find_appointment(appointments, "Zoo"))
Ziv Wu
  • 36
  • 3
0

What you need to use is a dictionary. So like:

appointments = {"Zoo":"11.03.22", "Shopping": "13.08.22"}
something_to_find = "Zoo"

Then to find something in the dictionary:

def find_something(lst, title=" "):
    print(lst[title])

find_something(appointments,something_to_find)

So yea you might want to read up on dictionaries. W3schools is a good place to start [https://www.w3schools.com/python/python_dictionaries.asp]

monos
  • 11
  • 2
  • Of course this would work, but that's not really the question. OP needs an answer that works with the way their list is constructed. – Swifty Oct 26 '22 at 16:36
  • Well I don't know OP so maybe they haven't come across dictionaries before and they can actually use a dictionary in their code instead of taking substrings out. – monos Oct 26 '22 at 16:48
0
def find_appointment(lst, title = ""):
     for i in lst:
         if title in i:
            a=title.find(title)
            return a
         else:
            print("No result")
appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22", 
"Funeral: 25.12.22"] 
print(find_appointment(appointments, "Zoo"))
#hope this helps. in order to iterate through all elements in list. we have to 
use a loop. once the value is found we can get its index by find method of 
string.