Add element to list without append() in Python

This tutorial will discuss about unique ways to append an element to list without append() in Python.

Table Of Contents

Method 1: Using += Operator

We can directly use the += operator to append an element into list. But the += operator accepts an another list on right hand side, when we we have a list on the left hand side. So, if we want to append a single element in list using it, then we need to encapsulate that element into square brackets to create a small list of one element only. Then apply += operator on it.

For example, we have a list of six numbers, and we want to add another number 99 into it. For that, we will create a new list containing number 99, and add that to the original list using += operator.

Let’s see the complete example,

listOfNumbers = [34, 90, 78, 33, 24, 11]

elem = 99

listOfNumbers += [elem]

print(listOfNumbers)

Output

[34, 90, 78, 33, 24, 11, 99]

Related Article

Python List – append() vs. extend()

So, basically we added a new element into the list, without use the append() function.

Method 2: Using insert() function

We can simply add an element at the end of list using the insert() function. It accepts two arguments,

  • Index position at which we want to insert the element. We will pass the size of list here, because we want to add element at the end of list.
  • Element to be added. We will pass a number 99 here.

It will add the given element 99, at the end of list.

Let’s see the complete example,

listOfNumbers = [34, 90, 78, 33, 24, 11]

elem = 99

listOfNumbers.insert(len(listOfNumbers), elem)

print(listOfNumbers)

Output

[34, 90, 78, 33, 24, 11, 99]

Related Article

Python : How to Insert an element at specific index in List ?

So, basically we added a new element into the list, without use the append() function.

Method 3: Using astrik to unpack list

Unpack all the elemnts of list as individual elements by applying astrik on it. Then add them into a new list along with the new element. Then assign them back to the same list variable. Basically, it is an indirect solution to add an element into a list without using the append() function.

Let’s see the complete example,

listOfNumbers = [34, 90, 78, 33, 24, 11]

elem = 99

listOfNumbers = [*listOfNumbers, elem]

print(listOfNumbers)

Output

[34, 90, 78, 33, 24, 11, 99]

So, basically we added a new element into the list, without use the append() function.

Summary

We learned about different solutions to add an element into a list without using the append() function.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top