Looking up a specific list value with Python


#1

Hey everyone,

I am working on building out some code that allows the user to lookup a password. So far I have been able to get the code to find if the password is currently stored in the list but not print out the password I am looking for. I either can get it to print out the website and passwords all at once, or just the websites that the passwords are stored on.

For example I have a list of websites and passwords for those sites:

passwords = [["msn","12343"],["abc","43521"],["yahoo","asdfg"]]

What I want to be able to do is prompt the user what website they are looking up the password for (accomplished) and after the input print out the website's password as saved from the list. The errors/roadblocks that I am facing are described above. Could anybody point me in the direction of where I am going wrong? I am specifically struggling to figure out how to call out within my print command the specific list item.

Here are a few of the codes I have tried:

print("Which website do you want to lookup the password for?")
        for keyvalue in passwords:
            print(keyvalue[0])
        passwordToLookup = input()

        for passwordToLookup in passwords:
          if passwordToLookup in passwords:
              print(int(passwords[0,passwordToLookup]))
          else:
              print("Login does not exist")
              break

print("Which website do you want to lookup the password for?")
        for keyvalue in passwords:
            print(keyvalue[0])
        passwordToLookup = input()

        for passwordToLookup in passwords:
          if passwordToLookup in passwords:
              print(str(passwordToLookup))
          else:
              print("Login does not exist")
              break

 print("Which website do you want to lookup the password for?")
        for keyvalue in passwords:
            print(keyvalue[0])
        passwordToLookup = input()

        for passwordToLookup in passwords:
          if passwordToLookup in passwords:
              print(passwords[0,passwordToLookup])
          else:
              print("Login does not exist")
              break

 print("Which website do you want to lookup the password for?")
        for keyvalue in passwords:
            print(keyvalue[0])
        passwordToLookup = input()

        for passwordToLookup in passwords:
          if passwordToLookup in passwords:
              print(int(passwords[0,passwordToLookup]))
          else:
              print("Login does not exist")
              break

   print("Which website do you want to lookup the password for?")
        for keyvalue in passwords:
            print(keyvalue[0])
        passwordToLookup = input()

        for passwordToLookup in passwords:
          if passwordToLookup in passwords:
              print(passwords(int([0,passwordToLookup])))
          else:
              print("Login does not exist")
              break

I can't really tell which iteration has me the closest or where that iteration may be going bad. As you can probably tell I've confused the heck out of myself here haha. Any tips or guidance is greatly appreciated!

Also, this is just one section of a very large application I am working on so there are some defined variables being used here that are worked on prior to this chunk of code... Just FYI.

Thanks,
Dev


#2

This really looks like a job for a dictionary, as opposed to a 2-D list.

vault = { "msn": "12343", "abc": "43521", "yahoo": "asdfg" }

It won't take any iteration to look up a password.

site = input("Enter the name of the site: ")
try:
    password = vault[site]
    print (password)
except KeyError:
    print ("Login does not exist")

Adding new entries is also very simple.

site  = input("Enter the name of the site: ")
password = input("Enter the password: ")
vault[site] = password

#3

Hey mtf, thank you for your help! I think my professor would rather us figure this out using a list instead of a dictionary. There is another section of the assignment for adding entries as well which I have already figured out and gotten to function properly. And he is cool with us using resources like this and collaborating with peers to work through problems so I promise I'm not cheating on my assignment haha.

Is there not an easy way to call out a specific section of a list that can update within a loop as you go?

Say for example you enter yahoo and the application then prints out the password for yahoo, then it loops and asks if you want to look up a password, add a new one, etc and you add a new one, say, gmail. Then it loops again and you decide to look up gmail and it prints that password. Does that make sense?

This is the resource he provided us with to work off of: http://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary ...ironically, the example is using a dictionary..


#4

This is the raw logic you could use:

>>> v = [["msn","12345"]]
>>> site = "msn"
>>> for i in v:
    if site in i:
        print (i[1])

12345
>>>

#5

So I got really excited for a second because I replaced your words with the ones that I am currently using and have:

passwordToLookup = input()

        for i in passwords:
          if passwordToLookup in i:
              print(i[1])
              break
          else:
              print("Login does not exist")
              break

And running it I typed in "yahoo" to check and it returned yahoo's password. However when I checked google it returned Login does not exist, which I am assuming is because the [1] is returning the password for only the first item in the list... I tried replacing the 1 with passwordToLookup hoping that ma do the trick but it didn't...

i.e.

if passwordToLookup in i:
              print(i[passwordToLookup])
              break
          else:
              print("Login does not exist")
              break

#6

The [1] is the index of i where the password is. [0] is the site name. If there is no entry for google in the list then it should come up with the default action. The thing to do is move the else over to the for block, not in the if block.

        for i in passwords:
            if passwordToLookup in i:
                print(i[1])
                break
        else:
            print("Login does not exist")

#7

Eureka! Thank you! I never really understood the 0 or 1 meanings either, so I appreciate that explanation as well!


#8

Hi, @dev3n ,

If you are working on a very large application, you may want to break it up into components, such as classes and functions, so that you can develop, test, and refine them as units. For example, you can include something like the following:

passwords = [["msn","12343"],["abc","43521"],["yahoo","asdfg"]]

class PasswordSystem(object):
    def __init__(self, password_list):
        self.password_list = password_list
    def get_password(self, account):
        for pair in self.password_list:
            if pair[0] == account:
                return pair[1]
        return False

p = PasswordSystem(passwords)
print("msn", p.get_password("msn"))
print("yahoo", p.get_password("yahoo"))
print("google", p.get_password("google"))

Output:

msn 12343
yahoo asdfg
google False

This makes it easier to refine individual components. For example, if later on your professor allows you to use a dictionary to store the passwords, as @mtf has suggested, and which would be better than using a list, you can modify the implementation of the PasswordSystem class accordingly.