Adding Tuple to List and vice versa in Python

PythonServer Side ProgrammingProgramming

When it is required to add tuple to list and do vice versa, the ‘+’ operator can be used.

Below is the demonstration of the same −

Example

 Live Demo

my_list = [3, 6, 9, 45, 66]

print("The list is : ")
print(my_list)

my_tup = (11, 14, 21)
print("The tuple is ")
print(my_tup)
my_list += my_tup

print("The list after addition is : " )
print(my_list)

Output

The list is :
[3, 6, 9, 45, 66]
The tuple is
(11, 14, 21)
The list after addition is :
[3, 6, 9, 45, 66, 11, 14, 21]

Explanation

  • A list is defined, and is displayed on the console.

  • A tuple is defined, and is displayed on the console.

  • The ‘+’ operator is used to add the list and tuple.

  • This is displayed as output on the console.

raja
Published on 17-Apr-2021 12:53:51
Advertisements