How do I return element from a list based on user input

Question:

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.

Asked By: HappilyStupid

||

Answers:

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]

Answered By: monkey_man

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"))
Answered By: Ziv Wu
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.
Answered By: Muzubul Rehman
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.