Adding Tuple to List and vice versa in Python


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.

Updated on: 17-Apr-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements