Created
December 14, 2022 00:22
-
-
Save hsleonis/88edf8cf6c54b7378a53ae22fef6fa2f to your computer and use it in GitHub Desktop.
Append to tuple in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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