The comments are running willy-nilly, but at least in the pre-view mode it helps to click "Toggle Plain Text".

if wish == 1:                                       #Menu option
        word = input("Enter word: ")
        description = input("Enter description: ")
        print
        print
        tuplelist.append((word,description))          #tuplelist is originally 
                                                                       defined as simply []
        main_dictuples(tuplelist)                          #Returns the function 
                                                                       for the main menu;
                                                                       ideally updating tuplelist 
                                                                       with a new tuple
                                                                       at the last position...

    if wish == 2:                                              #Menu option
        word = input("Enter word to look up: ")    #Shortened version, 
                                                                       but you get the idea
        if word in tuplelist:
            print "Yep"
        else:
            print "Nope"

Now, option 2 returns Nope regardless of wether the word you enter was entered into the list under option 1 before or not.
So what I'm trying to get to grips with is how to make python realise that the word entered is in the list. Or, to put it another way, to use the variable "word" under both 1 and 2 as a dictionary keyword.
Sheesh, I really have a long way to go...

Recommended Answers

All 6 Replies

when you go.

if word in tuplelist:
    #do stuff

That is a problem. The reason is your comparing something that looks like this:

"Hello"

To something that looks like this

[("hello","something you say as a greeting")]

Now those dont look quite the same right?
So what you need to do is say,

if True in [word == f[0] for f in tuplelist]:
    #do stuff

That might not be the most efficient way but it will work better.

Thanks for the quick reply, now at least I understand that much more. Just one more of those staring-at-semantics-error-not-getting-what-the-syntax-wants-problems:

As far as I understand, the operation you provided means that the program identifies the tuple we're after and thus, asking it to print f[1] should give the other half of the tuple, in this case the description. But say for example that I first enter ("Hello","Greeting") under option 1 and then also add in ("No","Refusal") and then ask it to look up the word "Hello":
It answers "Refusal".

I thought this setup would create a new tuple for each new entry? Or is it just me misunderstanding what the program does when looking up?
Sorry for the daft questions...

You could use this, if you want to access the tuples

# select the tuples which first element is word
selection = [item for item in tuplelist if item[0] == word]
if selection:
    print word, "was found!"
    print selection
else:
   print word, "was not found"

However, a more efficient way to do this would be to use dictionaries.

It would, this is just a matter of me wanting to understand this completely; the way I see it, you don't get good at things by ignoring the parts you don't grasp immediately.

Still, though, there seems to be things I mess up:

I've experimented quite a bit and I know that the list itself looks like it should. For example I did like this:

word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            print
            print
            print tuplelist 
        else:
            print "Nope"

... and it writes tuplelist exactly the way I want it. But it won't let me indetify the index of a particular tuple.

If I write like this:

word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            print
            print
            print tuplelist
            print
            print
            print item[1] 
        else:
            print "Nope"

... it evidently recognizes the word I entered (if it wasn't in the list it'd return "Nope", which it doesn't), and prints tuplelist looking good with the right word and description paired together. But it interprets item[1] to be the second part of the

last tuple.
Say for example I've made the three entries
"Hello" and "Greeting"
"No" and "Refusal"
"What" and "Wondering"
the snippet above will return

[('Hello', 'Greeting'), ('No', 'Refusal'), ('What', 'Wondering')]


Wondering

even if I told it to look for the word "Hello". Even if, and this is making me really lost in the fog, I do like this (the same entries in the list as before):

word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            a = tuplelist.index(item)
            print
            print
            print tuplelist[a]
        else:
            print "Nope"

it simply returns a variation on the above:
[('Hello', 'Greeting'), ('No', 'Refusal'), ('What', 'Wondering')]


('Va', 'Wondering')


So, well, um... help?

Don't use item after the selection=... . in this expression, item varies from the first element to the last element of the list. You can use selection , which is a list of all the tuples in tuplelist which first element is word. You could obtain a list of indexes instead, like this

indexlist = [ i for i in xrange(len(tuplelist)) if tuplelist[i][0] == word]

Well, I got something working, looks like this:

word = input("Enter word to look up: ")
        selection = [item for item in tuplelist if item[0] == word]
        if selection:
            a = selection[0] 
            print
            print
            print a[1]
            print
            print
        else:
            print "Nope"

Thanks for the input guys, it was very helpful, not to mention useful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.