How To Append A Tuple To A List In Python

In this article, we will explain to you how to Append a tuple to a list in Python, like using the append() method, extend() method, += operator, and using insert() method. Hopefully, through this article, you can easily find an easy way to do that.

To Append a tuple to a list in Python

Using the append() method

Firstly, the best way is to use the append() method to append a tuple to a list in Python. This method will add the element to the end of the list.

Code Example

Let’s see the code example below.

myList = []
myTupple = ('Ronaldo', 'Messi', 'Neymar', 'Benzema')

# Using the append() method to append a tuple into a list
myList.append(myTupple)
print("After appending myTupple into myList: ", myList)

Output

After appending myTupple into myList:  [('Ronaldo', 'Messi', 'Neymar', 'Benzema')]

Using the extend() method

The following way is using the extend() method to do it. This method will iterate over an integrable and then add each element to the end of the list.

Example

myList = []
myTupple = ('Ronaldo', 'Messi', 'Neymar', 'Benzema')

# Using the extend() method
myList.extend(myTupple)
print("After appending myTupple into myList: ", myList)

Output

After appending myTupple into myList:  ['Ronaldo', 'Messi', 'Neymar', 'Benzema']

Using the += operator

The next way is to use the += operator to append a tuple to a list in Python.

Example

In this example, we will use the += to add the myTupple to the last index in a list.

myList = []
myTupple = ('Ronaldo', 'Messi', 'Neymar', 'Benzema')

# Using the += operator to append a tuple into a list
myList += myTupple
print("After appending myTupple into myList: ", myList)

Output

After appending myTupple into myList:  ['Ronaldo', 'Messi', 'Neymar', 'Benzema']

Using the insert() method

The last way is using the insert() method. This method will insert the value to the specified position into the list.

Syntax: list.insert(i, ele)

Parameter :

  •             i: is the index you want to add the value to a list.
  •             ele: is an element you want to add to the list.

Example

In this example, we will use the insert(1, myTupple) to add the myTupple to the first index in a list.

myList = []
myTupple = ('Ronaldo', 'Messi', 'Neymar', 'Benzema')

# Using the insert() method to append a tuple to the first index of a list
myList.insert(0, myTupple)
print("After appending myTupple into myList: ", myList)

Output

After appending myTupple into myList:  [('Ronaldo', 'Messi', 'Neymar', 'Benzema')]

Summary

Throughout the article, we already introduce many methods to append a tuple to a list in Python, but using the append() method is the best way to do it. Leave your comment here if you have any questions about this article.

Thank you for reading!

Leave a Reply

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