6

Add element to list without append() in Python

 1 year ago
source link: https://thispointer.com/add-element-to-list-without-append-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Advertisements

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,

Advertisements

listOfNumbers = [34, 90, 78, 33, 24, 11]
elem = 99
listOfNumbers += [elem]
print(listOfNumbers)
listOfNumbers = [34, 90, 78, 33, 24, 11]

elem = 99

listOfNumbers += [elem]

print(listOfNumbers)

Output

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

Related Article

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)
listOfNumbers = [34, 90, 78, 33, 24, 11]

elem = 99

listOfNumbers.insert(len(listOfNumbers), elem)

print(listOfNumbers)

Output

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

Related Article

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)
listOfNumbers = [34, 90, 78, 33, 24, 11]

elem = 99

listOfNumbers = [*listOfNumbers, elem]

print(listOfNumbers)

Output

[34, 90, 78, 33, 24, 11, 99]
[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.

Advertisements

Thanks for reading.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK