Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created December 14, 2022 00:22
Show Gist options
  • Save hsleonis/88edf8cf6c54b7378a53ae22fef6fa2f to your computer and use it in GitHub Desktop.
Save hsleonis/88edf8cf6c54b7378a53ae22fef6fa2f to your computer and use it in GitHub Desktop.
Append to tuple in Python
# Creating a Tuple
a_tuple = (1, 2, 3)
# Method 1: Tuple Concatenation
a_tuple = a_tuple + (4,)
print(a_tuple)
# Returns: (1, 2, 3, 4) <- an entirely new object
# Method 2: List Conversion
a_list = list(a_tuple)
a_list.append(4)
a_tuple = tuple(a_list)
print(a_tuple)
# Returns: (1, 2, 3, 4)
# Method 3: Tuple Unpacking
a_tuple = (*a_tuple, 4)
print(a_tuple)
# Returns: (1, 2, 3, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment