ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Python program for adding a Tuple to List and Vice-Versa

Here, we have a list and we need to add a tuple to it which will result in a list. And for one more way, we will have a tuple and we need to add a list to it which will result in a tuple.
Submitted by Shivang Yadav, on June 05, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Adding a Tuple to a list

We have a list of elements and we will be adding a tuple to this list and then returning back a tuple consisting of all elements in a list.

Example:

Input:
myList = [3, 6, 1] , myTuple = (2, 9, 4)

Output:
[3, 6, 1, 2, 9, 4]

We can add a tuple to a list by taking the list and then adding the tuple value using += operator or list.extend() method to add the tuple at the end of our list.

Syntax:

  • += Operator: obj1 += obj2
  • extend() method: list_name.extend(collection)

Program to add a tuple to a list in Python

# Python program to add a tuple to list 

# Creating the List
myList = [9, 3, 1, 4]

# Printing the List 
print("Initially List : " + str(myList))

# Creating Tuple
myTuple = (2, 6)

# Adding the tuple to list
myList += myTuple

# Printing resultant List
print("List after Addition : " + str(myList))

Output:

Initially List : [9, 3, 1, 4]
List after Addition : [9, 3, 1, 4, 2, 6]

Adding a list to a Tuple

In a similar way as we saw above, we can add a list to a tuple by first converting the tuple to a list then adding the list to it. And then converting the resulting list back to tuple.

Program to add a list to tuple in Python

# Python program to add a tuple to list 

# Creating the List
myTuple = (9, 3, 1, 4)


# Printing the List 
print("Tuple Initially : " + str(myTuple))

# Creating Tuple
myList = [2, 6]

# Adding the tuple to list
addList = list(myTuple)
addList += myList
myTuple = tuple(addList)

# Printing resultant List
print("Tuple after Addition : " + str(myTuple))

Output:

Tuple Initially : (9, 3, 1, 4)
Tuple after Addition : (9, 3, 1, 4, 2, 6)

Python Tuple Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.