Creating a Set from user Input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 24, 2022

banner

Photo from Unsplash

Creating a Set from user Input in Python #

To create a set object from user input:

  1. Use the input() function to take input from the user.
  2. Use the str.split() function to split the input string into a list.
  3. Use the set() class to convert the list to a set object.
main.py
import ast # ✅ create a set from string user input my_set = set(input('Enter space-separated words: ').split()) print(my_set) # ------------------------------- # ✅ create a set from integer user input user_input = input('Enter space-separated integers: ') my_set = set(int(item) for item in user_input.split()) print(my_set) # ------------------------------- # ✅ create a set from user input try: input_list = ast.literal_eval( input('Enter a valid Python set, e.g. {"a", "b"}: ') ) except ValueError: print('The provided value is not a set object') print(input_list) # 👉️ ('a', 'b')

create set from user input

We used the input() function to take input from the user.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

Note that the input() function returns a string even if the user enters a number.

We used the str.split() method to split the user input string on each whitespace character.

main.py
my_set = set(input('Enter space-separated words: ').split()) print(my_set)

The str.split() method splits the string into a list of substrings using a delimiter.

When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.

You can also use another string separator, e.g. a comma.

main.py
my_set = set(input('Enter comma-separated words: ').split(',')) print(my_set)

create set from user input comma separator

The str.split() method splits the input string on each comma.

If you need to create a set of integers from user input, use a generator expression to convert each input string to an integer.

main.py
user_input = input('Enter space-separated integers: ') my_set = set(int(item) for item in user_input.split()) print(my_set)

create set of integers from user input

We used the str.split() function to split the input string on each whitespace character.

We then used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the int() class to convert the input string to an integer.

Alternatively, you can use the ast.literal_eval method.

main.py
import ast try: input_list = ast.literal_eval( input('Enter a valid Python set, e.g. {"a", "b"}: ') ) except ValueError: print('The provided value is not a set object') print(input_list) # 👉️ ('a', 'b')

create set from user input literal

The ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dicts, sets, booleans and None.

This assumes that the expected input is a valid Python set, e.g. {1, 2, 3}.

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.