1

I´m trying to build an Address Book and the propose to create a Search function is so that it can present the Phone Number and Email of that "Name" (Person).

The person was added before so it looks like this:

 lst = [['Robert', '12', 'robert@live.com'], ['Steve', '1', 'steve@limework.com']]

and I want to locate Robert (per example) and print his number (12) and email (robert@live.com).

At the moment my Code looks like this:

def searach(lst):
    name=str(input("Name:"))
    for i in range(len(lst)):
        for i in range(len(lst)):
        if name == lst[i]:
            m=i
            print(lst[m][1]) and print(lst[m][2])
        break
        else:
            print("Name doesn´t exist!")

When I do print(len(lst)) it return a value of 0 so it can´t iterate over the list in order to provide me the phone and email I need but when I do print(lst[0][1]) and print(lst[0][2]) it returns the correct Phone and Email of the 1st person on the list in this case Robert.

My post is different from the one suggested since I have 3 variables.

Phyti
  • 131
  • 9
  • 2
    I strongly encourage you to implement your phonebook as a dictionary. The list approach is a little... unsophisticated. – timgeb Dec 23 '15 at 00:23
  • @timgeb I´m really trying to figure out if this could be done with lists, perhaps using lambda functions. – Phyti Dec 23 '15 at 00:27
  • @KarolyHorvath what kind of info do you need more? – Phyti Dec 23 '15 at 00:30
  • @timgeb: Or use a DB, even something as simple as `sqlite3`, so you can query on any field to find the others. – ShadowRanger Dec 23 '15 at 00:30
  • @Phyti: There are a very small number of problem types where `lambda`s are the correct solution. If you set out to solve a problem with `lambda`s, you're going about it the wrong way. – ShadowRanger Dec 23 '15 at 00:31

1 Answers1

0

Note that you are using and incorrectly. print returns None, so Python will not print the second part.

I would use a fast list comprehension to find the matching item(s):

matches = [sublist for sublist in lst if sublist[0] == name]

In this instance, matches would be

[['Robert', '12', 'robert@live.com']]

One way to output, allowing for the possibility of multiple matches:

if matches:
    for i in matches:
        print(i[1], i[2])
else:
    print('Not found')
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
  • 1
    Huh, how did I get that in there after it was closed? – Tom Zych Dec 23 '15 at 00:33
  • What do you call sublist? – Phyti Dec 23 '15 at 00:46
  • It's just a sort of placeholder variable, with no meaning outside the list comprehension ... and I realize I got the syntax wrong, shooting from the hip. – Tom Zych Dec 23 '15 at 00:47
  • So matches would be my list like this: lst= [sublist for sublist in lst if sublist[0] == name] if lst: for i in lst: print(i[1], i[2]) else: print('Not found') – Phyti Dec 23 '15 at 00:50
  • Sorry, I don't understand what you mean. – Tom Zych Dec 23 '15 at 00:59
  • What are you calling matches? the elements on the list? my list is = to this :lst = [['Robert', '12', 'robert@live.com'], ['Steve', '1',steve@limework.com']] So is this matches instead of lst? – Phyti Dec 23 '15 at 01:21
  • We start with `lst`, which contains all the entries. We then use the list comprehension to build a new list containing only the matching sublists, and point the name `matches` at the new list. `matches` is also a list of lists, but only those lists where the first part is `'Robert'` (or whatever). – Tom Zych Dec 23 '15 at 01:30
  • I have implemented it and doesn´t work it always prints Not Found even if the name is there. – Phyti Dec 23 '15 at 01:46