-2

I'm trying to set a tuple of integers as the key for my dictionary. Here is the relevant code:

class Solution:       
def longestPalindrome(self, s):
    """
    :type s: str
    :rtype: str
    """
    paldict = {}
    stringlen = len(s)
    for i in range(len(s)):
        if self.isPalindrome(s[i]) == True:
            paldict[(i, i)] = True
        else:
            paldict[(i, i)] = False
    for key, value in paldict:
        print(key)
        print(value)

The second for loop is just for testing because the compiler was telling me that when I tried to access the second element, that int types are not subscriptable which to me was strange. That was an error which should only occur if the type wasn't a tuple. Upon printing out, I saw that the keys were actually only a single integer instead of a tuple. Also, the value wasn't True or False, but the same integer again. Any ideas why?

Thunderpurtz
  • 189
  • 2
  • 12
  • 1
    `for key, value in paldict` is not how you iterate over keys and values. – user2357112 Dec 22 '18 at 03:35
  • What would be the proper way to do so? And my original question still stands, the tuple is not being set as the key. – Thunderpurtz Dec 22 '18 at 04:28
  • "Why isn't the tuple being set as the key?" - the fact that you think it's not being set as the key indicates that you didn't really read and understand the dupe target. – user2357112 Dec 22 '18 at 04:34
  • Interesting. Any idea why "for key in dict" produces the desired tuple, but "for key, value in dict" does not? That part I did not completely get from reading the duplicate. – Thunderpurtz Dec 22 '18 at 04:50
  • I think you'd have to do "for key, value in dict.items() to get the proper list of key value pairs. Then what exactly is "for key, value in dict" without the call to .items() doing then? – Thunderpurtz Dec 22 '18 at 04:55
  • It does pretty much the same thing as `for thing in dict: key, value = thing`. Do you know what `a, b = c` does when `c` is a tuple? – user2357112 Dec 22 '18 at 05:09

1 Answers1

1
for key, value in paldict.items():
    print(key)
    print(value)

You are missing .items()

ycx
  • 3,155
  • 3
  • 14
  • 26