Back to questions
JuniorTheoryCommon
What does the program print?
words = ["apple", "avocado", "banana"]
first = {w[0]: w for w in words}
print(len(first), first["a"])2 avocado
- Expecting
apple, as if the first key wins: each new pair with the key"a"replaces the old value, so the last such word,avocado, stays. - Expecting
3entries: a dict holds each key once, andappleandavocadoshare the key"a", so only2keys remain, with no error.
- How would you keep the first word for each letter instead?
- What does
{len(w) for w in words}produce?