How to Create a Tuple from a String and a List of Strings

Tuples, Strings, and Lists

Before we look at creating tuples, as a starting point it would be useful to clarify the meaning of the various terms we will be using throughout this article: 

  • A string is a sequence of characters, and represents one of the main Python data types.
  • A list is used to store multiple items in a single variable, a list allows duplicate values, is ordered, and is mutable. List items can be different data types.
  • A tuple is also used to store multiple items in a single variable, a tuple allows duplicate values, is ordered, and is immutable. Tuple items can be different data types.

As you can see, in most respects, tuples and lists are identical, but they differ in two key properties: 

  • Tuples are defined by enclosing the items in parentheses (), whereas lists use square brackets []
  • Tuples are immutable, meaning once created they can not be changed. Lists on the other hand are mutable, meaning they can be changed once created.

The tuple() Function

So, now we have clarified the terminology we can look at creating tuples, and luckily for us Python has a built-in function for this very purpose: tuple()

There are two ways we can use this function, so let’s start by creating a simple string and a list of strings

my_string = 'Hello'
my_list = ('today', 'is', 'Wednesday')

And we can confirm they are indeed the correct type as follows: 

print(type(my_string))
# <class 'str'>

print(type(my_list))
# <class 'list'>

List Conversion

By default the tuple() function turns a list object into a tuple, so to make this work for our example we need to:

  1. Convert our string object to a list, by enclosing it in square brackets [] 
  2. Append this new list object to our existing list(my_list) with β€˜+’
  3. Convert the resulting list to a tuple by passing it into the tuple() function
res = tuple([my_string] + my_list)
print(res)
# ('Hello', 'today', 'is', 'Wednesday')

And just to confirm we do indeed have a tuple: 

print(type(res))
# tuple

Tuple Conversion

Alternatively we can convert both our string and list directly to a tuple as follows: 

>>> res1 = (my_string,) + tuple(my_list)
>>> res1
('Hello', 'today', 'is', 'Wednesday')

And just to confirm we do indeed have a tuple: 

>>> type(res1)
tuple

As you may have noticed, with this method we have not converted our string to a list object and neither have we explicitly rendered it a tuple by passing it through the tuple() function. In addition you have probably noticed the comma β€˜,’ after the my_string variable. That is not a typo!  This is all due to the way Python treats single items in a tuple. 

Creating a Tuple from a Single Item

To understand this, let’s see what would happen if we passed a single string item, in our case the my_string variable, through our tuple function, without the comma: 

>>> single = tuple(my_string)
>>> single
('H', 'e', 'l', 'l', 'o')

As you can see, Python has assumed that each character of the string represents an individual item. To ensure that our  my_string variable is treated as one item, we need to add the comma: 

>>> single = tuple(my_string,)
>>> single
('H', 'e', 'l', 'l', 'o')

Okay, so that wasn’t the result we were expecting either! Again, this is down to the way Python creates tuples with single items – irrespective of whether it is a string, an integer or a float. Simply by adding the comma after our item, Python knows it is a tuple. By explicitly passing this through the tuple() function, Python assumes each character should be treated as a separate element. Therefore, if we have a single item and we want to create a tuple we just have to put a comma after it, we don’t even need the parentheses: 

>>> single = my_string,
>> single
('Hello',)

And we can confirm that this item is indeed a tuple: 

>>> type(single)
tuple

The Use of Parentheses () 

This brings us nicely on to the last topic in this article, the use of parentheses to create a tuple. As we have seen when creating a tuple from a single item, the parentheses are very much optional. Python will create a tuple purely based on the use of the comma after our item:  

single = my_string,

Is the same as: 

single = (my_string,)

Similarly we can create a tuple directly by using the comma to separate multiple items: 

my_tuple = 'Hello', 'today', 'is', 'Wednesday'

print(my_tuple)
# ('Hello', 'today', 'is', 'Wednesday')

print(type(my_tuple))
# tuple

It is therefore, the comma that creates the tuple in Python not the parentheses.  

Summary

As we have only focused on creating a tuple from a string and a list of strings, the tuple() function has proved perfect. It is logical and straightforward, and the difference between the List Conversion and Tuple Conversion methods is minimal. Although, if it is speed you are after the Tuple Conversion method is apparently faster. 

If you want to create a tuple from a single item, be it a string, integer or float we have seen that the comma is what actually tells Python we want to create a tuple. In fact, we have seen that we can use the comma on single and multiple items to create a tuple, even without parentheses. 

Whilst the use of parentheses in these instances is indeed optional, it’s good practice to include them anyway – so anyone reading your code knows you explicitly wanted to create a tuple and to avoid any ambiguity.