Create a tuple from a string and a list of strings in Python

avatar

Borislav Hadzhiev

Last updated: Jul 2, 2022

banner

Photo from Unsplash

Create a tuple from a string and a list of strings in Python #

To create a tuple from a string and a list of strings:

  1. Use a trailing comma to convert the string to a tuple.
  2. Use the tuple() class to convert the list to a tuple.
  3. Use the addition (+) operator to combine the two tuples.
main.py
my_str = 'one' my_list = ['two', 'three'] my_tuple = (my_str,) + tuple(my_list) print(my_tuple) # 👉️ ('one', 'two', 'three')

Notice that we used a trailing comma to convert the string to a tuple with one element.

As the documentation states construction of tuples with 1 item is done by following a value with a comma.

main.py
my_str = 'one' tuple_1 = (my_str,) print(tuple_1) # 👉️ ('one', ) print(type(tuple_1)) # 👉️ <class 'tuple'>
Note that enclosing the value in parentheses is not required, but makes your code more readable.

It is not sufficient to wrap a single value in parentheses to create a tuple.

The trailing comma after the value is the difference between creating a tuple (with trailing comma) or creating a value of a different type (without trailing comma).

The next step is to use the tuple() class to convert the list to a tuple.

main.py
my_str = 'one' my_list = ['two', 'three'] tuple_1 = (my_str,) tuple_2 = tuple(my_list) print(tuple_2) # 👉️ ('two', 'three')

The tuple class takes at most 1 argument - an iterable and converts the iterable to a tuple.

We didn't use the tuple() class to convert the string to a tuple because passing a string to the class would get us a tuple containing the string's characters.

main.py
my_str = 'one' print(tuple(my_str)) # 👉️ ('o', 'n', 'e')

The last step is to use the addition operator to combine the two tuples.

main.py
my_str = 'one' my_list = ['two', 'three'] tuple_1 = (my_str,) tuple_2 = tuple(my_list) my_tuple = tuple_1 + tuple_2 print(my_tuple) # 👉️ ('one', 'two', 'three')

When used with two or more tuples, the addition (+) operator combines the tuples.

main.py
# 👇️ ('one', 'two', 'three') print(('one',) + ('two', 'three'))

Alternatively, you can wrap the string in square brackets and use the tuple() class.

To create a tuple from a string and a list of strings:

  1. Wrap the string in square brackets to get a list.
  2. Use the addition operator to combine the two lists.
  3. Use the tuple() class to convert the result to a tuple.
main.py
my_str = 'one' my_list = ['two', 'three'] my_tuple = tuple([my_str] + my_list) print(my_tuple) # 👉️ ('one', 'two', 'three')

The values on the left-hand and right-hand sides of the addition (+) operator are lists, so the two lists get merged.

main.py
my_str = 'one' my_list = ['two', 'three'] # 👇️ ['one', 'two', 'three'] print([my_str] + my_list)

The last step is to pass the list to the tuple() class to convert it to a tuple.

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.