Simplify, simplify, simplify:

Copydef p1(args):
    whatever

def p2(more args):
    whatever

myDict = {
    "P1": p1,
    "P2": p2,
    ...
    "Pn": pn
}

def myMain(name):
    myDict[name]()

That's all you need.


You might consider the use of dict.get with a callable default if name refers to an invalid function—

Copydef myMain(name):
    myDict.get(name, lambda: 'Invalid')()

(Picked this neat trick up from Martijn Pieters)

Answer from S.Lott on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_ref_dictionary.asp
Python Dictionary Methods
Python has a set of built-in methods that you can use on dictionaries.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-methods
Python Dictionary Methods - GeeksforGeeks
Python dictionary methods is collection of Python functions that operates on Dictionary. Python Dictionary is like a map that is used to store data in the form of a key: value pair.
Published   July 23, 2025
Discussions

Dict methods
Maybe I’m dumb, but “for each in dict” seems harder to me than simply ranging through my dict. Dictionaries don’t have indexes, they have keys. There’s nothing to “range through.” More on reddit.com
🌐 r/learnpython
19
1
October 14, 2022
python - Using a dictionary to select function to execute - Stack Overflow
I know, it was my first choice, but I want the final user to have a limited access, so the user can't change the contents in the dictionary in run-time. 2012-02-06T22:41:05.96Z+00:00 ... The user can always change anything they want at run-time. It's Python. They have the source. More on stackoverflow.com
🌐 stackoverflow.com
dictionary - Not understanding a trick on .get() method in python - Stack Overflow
While learning python I came across a line of code which will figure out the numbers of letters. dummy='lorem ipsum dolor emet...' letternum={} for each_letter in dummy: letternum[each_letter. More on stackoverflow.com
🌐 stackoverflow.com
Dictionary functions
I think it should be added a function of append() in ditionary :smiley: More on discuss.python.org
🌐 discuss.python.org
0
February 11, 2023
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated ...
🌐
Cisco
ipcisco.com › home › python dictionary methods
Python Dictionary Methods | get() | keys() | values()
April 14, 2023 - In this Python Dictionary Methods, we will learn different methods used with python dictionaries. We will learn get, keys, values, items etc.
🌐
Reddit
reddit.com › r/learnpython › dict methods
r/learnpython on Reddit: Dict methods
October 14, 2022 -

Kind of ranting about dictionary methods:

Why does keys() not return a simple list. I nearly broke my head today, because I could not figure out how to iterate through my dict without using "each in dict".

Maybe I'm dumb, but "for each in dict" seems harder to me than simply ranging through my dict. And yeah, like 10h in I find list(dict.keys()) is a viable solution...

Second one: why does nobody mention "dict[x] =+ value" does exist? I had a very hard time finding this. Maybe too basic? I don't know.

Not even mentioning "del", which works totally different than other methods, imho.

Could somebody make me feel less dumb? Are dictionarys the lower lifeform of data in python?

🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
5 days ago - The dict object is the dictionary class. See dict and Mapping Types — dict for documentation about this class. For other containers see the built-in list, set, and tuple classes, as well as the collections module. ... Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object. If the object has a method named __dir__(), this method will be called and must return the list of attributes.
Find elsewhere
🌐
Programiz
programiz.com › python-programming › dictionary
Python Dictionary (With Examples)
March 26, 2024 - Here are some of the commonly used dictionary methods. We can check whether a key exists in a dictionary by using the in and not in operators.
🌐
freeCodeCamp
freecodecamp.org › news › python-dictionary-methods-dictionaries-in-python
Python Dictionary Methods – Dictionaries in Python
July 28, 2022 - In this article, I will show you how to create a dictionary in Python and work with it using those methods.
🌐
Medium
medium.com › @shilpasree209 › the-keys-values-and-items-methods-in-python-dictionary-4c24cc3d26a7
The keys(), values() and items() Methods in Python Dictionary | by Shilpa Sreekumar | Medium
November 24, 2023 - In Python dictionaries, there are three dictionary methods that will return list like values of the dictionary’s keys, values or both keys and values: keys(), values() and items().
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dict-function
Python dict() Function - GeeksforGeeks
July 23, 2025 - It is highly efficient for lookups, insertions, and deletions due to its underlying hash table implementation. Dictionaries are versatile for managing data with unique identifiers and are dynamically resizable. ... The dict.items() method returns a view object that displays a list of the dictionary’s key-value pairs.
Top answer
1 of 6
11

The get method on a dictionary is documented here: https://docs.python.org/3/library/stdtypes.html#dict.get

get(key[, default])

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

So this explains the 0 - it's a default value to use when letternum doesn't contain the given letter.

So we have letternum.get(each_letter, 0) - this expression finds the value stored in the letternum dictionary for the currently considered letter. If there is no value stored, it evaluates to 0 instead.

Then we add one to this number: letternum.get(each_letter, 0) + 1

Finally we stored it back into the letternum dictionary, although this time converting the letter to lowercase: letternum[each_letter.lower()] = letternum.get(each_letter, 0) + 1 It seems this might be a mistake. We probably want to update the same item we just looked up, but if each_letter is upper-case that's not true.

2 of 6
3

letternum is a dict (a dictionary). It has a method called get which returns the value associated with a given key. If the key is absent from the dictionary, it returns a default value, which is None unless an optional second argument is present, in which case that argument value is returned for missing elements.

In this case, letternum.get(each_letter,0) returns letternum[each_letter] if each_letter is in the dictionary. Otherwise it returns 0. Then the code adds 1 to this value and stores the result in letternum[each_letter.lower()].

This creates a count of the number of occurrences of each letter, except that it inconsistently converts the letter to lowercase when updating, but not when retrieving existing values, so it won't work properly for uppercase letters.

🌐
Programiz
programiz.com › python-programming › methods › dictionary
Python Dictionary Methods | Programiz
In this reference page, you will find all the methods to work with dictionaries.
🌐
Tutorials
zframez.com › tutorials › chapter 9: python dictionaries – operations and methods
Chapter 9: Python Dictionaries - Operations and Methods - Tutorials
October 16, 2024 - Learn how to work with dictionaries in Python. This chapter covers creating dictionaries, adding and removing key-value pairs, dictionary methods, and two-dimensional dictionaries
🌐
Python.org
discuss.python.org › python help
Dictionary functions - Python Help - Discussions on Python.org
February 11, 2023 - I think it should be added a function of append() in ditionary 😃
🌐
Scientech Easy
scientecheasy.com › home › blog › python dictionary methods
Python Dictionary Methods - Scientech Easy
May 22, 2023 - This method returns a list of key-value pairs in tuple from the dictionary. The basic syntax to define this method is as: ... # Python program to get a list of dictionary elements. my_dict = {1: "Mahika", 2: "Ivaan", 3: "Mark", 4: "Bob"} # Call items() method to get the dictionary elements in tuple form.
🌐
DEV Community
dev.to › usooldatascience › a-quick-guide-to-python-dictionary-methods-with-examples-2gfb
A Quick Guide to Python Dictionary Methods with Examples - DEV Community
September 11, 2024 - d = {'a': 1, 'b': 2} 'a' in d # True 'c' in d # False · Deletes a key-value pair from the dictionary. ... Python dictionaries provide a wide range of methods for key-value management.
🌐
Real Python
realpython.com › python-dicts
Dictionaries in Python – Real Python
December 16, 2024 - Python dictionaries have several methods that you can call to perform common actions like accessing keys, values, and items. You’ll also find methods for updating and removing values. In the following sections, you’ll learn about these methods and how to use them in your Python code.
🌐
FavTutor
favtutor.com › blogs › python-dictionary
Python Dictionary: Methods & Operations (with Examples)
October 6, 2023 - Additionally, one can iterate over the values in a dictionary by applying the values() method, which returns a list of all values. The subsequent code prints all values in my_dictionary: ... Apart from the fundamental operations, Python dictionaries offer numerous methods.
🌐
Codecademy
codecademy.com › learn › learn-python-3 › modules › learn-python3-dictionaries › cheatsheet
Learn Python 3: Dictionaries Cheatsheet | Codecademy
Python provides a .get() method to access a dictionary value if it exists. This method takes the key as the first argument and an optional default value as the second argument, and it returns the value for the specified key if key is in the ...