Warning: session_start(): open(/tmp/sess_cb7bbe2dfc6352a30997aaea25478b9a, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Insert An Element Into A Tuple In Python - LearnShareIT

How To Insert An Element Into A Tuple In Python

Insert an element into a Tuple in Python

To insert an element into a Tuple in Python, you convert a tuple to a list and add an element on the list, do the same on set or use the addition operator. Please read the following article for details.

Insert an element into a Tuple in Python

Convert tuple to list

Example:

  • Convert tuple to list.
  • Use the insert() function to add an element to the list.
  • Use the tuple() function to convert the list back to a tuple.
myTuple = ('Visit', 'Learnshareit', 'Website')
print('Initial tuple:', myTuple)

# Convert the tuple to a list using the list() function.
myList = list(myTuple)

# Add an element to the list by the insert() function.
myList.insert(3, '!')

# Convert the list back to a tuple using the tuple() function.
result = tuple(myList)
print('Tuple after adding element:', result)

Output:

Initial tuple: ('Visit', 'Learnshareit', 'Website')
Tuple after adding element: ('Visit', 'Learnshareit', 'Website', '!')

You can change the position of the element to be inserted.

Example:

myTuple = ('Visit', 'Learnshareit', 'Website')
print('Initial tuple:', myTuple)

# Convert the tuple to a list using the list() function.
myList = list(myTuple)

# Add an element to the list by the insert() function.
myList.insert(1, '!')

# Convert the list back to a tuple using the tuple() function.
result = tuple(myList)
print('Tuple after adding element:', result)

Output:

Initial tuple: ('Visit', 'Learnshareit', 'Website')
Tuple after adding element: ('Visit', '!', 'Learnshareit', 'Website')

Or add the element to the end of the list by append() and convert it back to a tuple.

Example:

myTuple = ('Visit', 'Learnshareit', 'Website')
print('Initial tuple:', myTuple)

# Convert the tuple to a list using the list() function.
myList = list(myTuple)

# Add an element to the end of the list using the append() function.
myList.append('!')

# Convert the list back to a tuple using the tuple() function.
result = tuple(myList)
print('Tuple after adding element:', result)

Output:

Initial tuple: ('Visit', 'Learnshareit', 'Website')
Tuple after adding element: ('Visit', 'Learnshareit', 'Website', '!')

Convert tuple to set

Example:

  • Convert the tuple to set by the set() function.
  • Use the add() method to add an element to the set.
  • Convert back to a tuple using the tuple() function.
myTuple = ('Visit', 'Learnshareit', 'Website')
print('Initial tuple:', myTuple)

# Convert the tuple to a set using the set() function.
myList = set(myTuple)

# Add an element to the set by the add() function.
myList.add('!')

# Convert the set back to a tuple using the tuple() function.
result = tuple(myList)
print('Tuple after adding element:', result)

Output:

Initial tuple: ('Visit', 'Learnshareit', 'Website')
Tuple after adding element: ('Learnshareit', 'Visit', '!', 'Website')

Note: When using the add() method, the elements will not be sorted further.

Use the addition operator to add two tuples

This is probably not considered an orthodox way to add an element to a tuple, but you can use it, and as a result, it is still correct.

Example:

  • Declare two tuples.
  • Use the plus operator to add an element to ‘firstTuple’.
firstTuple = ('Visit', 'Learnshareit')
secondTuple = ('website',)

# Use the addition operator to add an element to 'firstTuple'.
firstTuple = firstTuple + secondTuple
print('Tuple after adding element:', firstTuple)

Output:

Tuple after adding element: ('Visit', 'Learnshareit', 'website')

Note: If you initialize a tuple with only one element, you need to put a comma after that element; otherwise, the compiler will mistake it for a string and throw the error TypeError: can only concatenate tuple (not “str”) to tuple.

Summary

Those are the ways to insert an element into a Tuple in Python. You can use one of the three methods above because it is pretty easy to understand using only essential data types. If there is a better way, please comment below. We appreciate it.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *