Warning: session_start(): open(/tmp/sess_6c7674ecd5406689d6a4c053499f2025, 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 Add An Element To A Tuple In Python - LearnShareIT

How To Add An Element To A Tuple In Python

Add an element to a tuple in Python

There are some ways to add an element to a tuple in Python. We have some methods, such as create a new tuple or use the list() method and tuple() method. Let’s learn about it with the explanation and examples below.

Add an element to a tuple in Python

Create a new tuple 

Tuples() are used to store multiple items in only a variable. Tuples() is already built in Python and is a collection that is ordered and unchangeable. 

You can add a new element to a tuple in Python by creating a new tuple and then adding two tuples together. More details are below example. 

Example: 

In this example, we create a new tuple with Shark element, then we can easily add two tuples to insert a new element to the animal’s tuple

# Create a new tuple
animalsTuple = ("Lion", "Tiger", "Elephant", "Dolphin")
print(animalsTuple)

newAnimal = ("Shark",)

# Add two tuples to insert a new element to the animal's tuple
animalsTuple = animalsTuple + newAnimal

print("add 'Shark' in to animals tuple")
print(animalsTuple)

Output:

('Lion', 'Tiger', 'Elephant', 'Dolphin')
add 'Shark' to animals tuple
('Lion', 'Tiger', 'Elephant', 'Dolphin', 'Shark')

Use list() method, tuple() method

In other ways, you can convert the tuple to a list, and then use insert() to add a new element to the list(), then use the tuple() method to convert the list to the tuple().

List() method

Syntax: list(iterable)

Parameter:

  • iterable: a sequence, collection, or an iterator object.

Tuple() method

Syntax:

tuple(iterable)

Parameter:

  • iterable: (optional) – an iterable (list, range, etc.) or an iterator object.

If the iterable is not passed to tuple(), the function returns an empty tuple.

Example:

In this example, we will convert animals_tuple into a list by using the list() method. Then add Shark element to the list by using the insert() method. Finally, we convert the list to tuple by using the tuple() method.

# Create a new tuple
animalsTuple = ("Lion", "Tiger", "Elephant", "Dolphin")
print(animalsTuple)

# Convert tuple animalsTuple to list listAnimals by using list()
listAnimals = list(animalsTuple)

# Add 'Shark' to position 4 in listAnimals
listAnimals.insert(4, "Shark")
print("add 'Shark' to list animals")
print(listAnimals)

# Convert listAnimals to tuple
list2tupleAnimals = tuple(listAnimals)
print("after converting listAnimals to tuple")
print(list2tupleAnimals)

Output:

('Lion', 'Tiger', 'Elephant', 'Dolphin')
add 'Shark' to list animals
['Lion', 'Tiger', 'Elephant', 'Dolphin', 'Shark']
after converting list_animals to tuple
('Lion', 'Tiger', 'Elephant', 'Dolphin', 'Shark')     

Summary

Our tutorial has guided you through different ways to add an element to a tuple in Python. We always hope this tutorial is helpful to you. If you have any questions, please let us know by leaving your comment in this tutorial. Thank you for your reading!

Maybe you are interested:

Leave a Reply

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