Using user input to select an option from a list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 23, 2022

banner

Photo from Unsplash

Using user input to select an option from a list in Python #

To use user input to select an option from a list:

  1. Construct a message that contains the options.
  2. Use the input() function to take user input.
  3. Check if the user entered one of the possible options.
main.py
options = ['python', 'js', 'ts'] user_input = '' input_message = "Pick an option:\n" for index, item in enumerate(options): input_message += f'{index+1}) {item}\n' input_message += 'Your choice: ' while user_input.lower() not in options: user_input = input(input_message) print('You picked: ' + user_input)

select option input from list

The example lets the user select one of the options in the list.

The user is expected to type the word, e.g. python, js, ts, but you could also tweak this and have the user type a number that corresponds to a list item.
main.py
options = ['python', 'js', 'ts'] user_input = '' input_message = "Pick an option:\n" for index, item in enumerate(options): input_message += f'{index+1}) {item}\n' input_message += 'Your choice: ' while user_input not in map(str, range(1, len(options) + 1)): user_input = input(input_message) print('You picked: ' + options[int(user_input) - 1])

select option input from list using integers

The code snippet above expects that the user enters a number that corresponds to the list item.

We used a formatted string literal to construct the input message.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second is the item.

main.py
my_list = ['apple', 'banana', 'melon'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 apple, 1 banana, 2 melon

On each iteration, we add 1 to the index to have the options start at 1.

main.py
options = ['python', 'js', 'ts'] user_input = '' input_message = "Pick an option:\n" for index, item in enumerate(options): input_message += f'{index+1}) {item}\n' input_message += 'Your choice: ' while user_input not in map(str, range(1, len(options) + 1)): user_input = input(input_message) print('You picked: ' + options[int(user_input) - 1])
We used a while loop to keep iterating until the selected option is one of the values in the list or one of the corresponding numbers.

Once the user picks a valid option, the condition is no longer met and we exit out of the while loop.

Alternatively, you can use the inquirer package.

Open your terminal in your project's root directory and install inquirer.

shell
pip install inquirer

Now we can import and use the inquirer package.

main.py
import inquirer options = ['python', 'js', 'ts'] questions = [ inquirer.List('language', message="What is your favorite language?", choices=options, ), ] answers = inquirer.prompt(questions) print(answers) print(answers['language'])

You can use the arrow keys on your keyboard to select one of the options.

python select option input inquirer

Note that the inquirer package is mainly intended to be used on Unix-based platforms (Linux and MacOS).

The package has experimental support for Windows.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.