Python

Append, insert and remove elements of lists in Python

We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

The elements of a list can be added or removed after it has been created.

  1. Add elements into a list

To add new elements at the end of a list, Python provides both functions. append(). Following code shows an empty list ‘cars’ is created first, then two cars ‘Volvo’ and ‘Benz’ are append one at a time to the list.

#to create an empty list
cars = []

#items are appended onto the list
cars.append('Volvo')
cars.append('Benz')

#show the list
cars

Out[1]: ['Volvo', 'Benz']

New elements can also be added into the specified position of an existing list using insert() function. In the following code, a new element ‘BMW’ was added onto the second place of the list, and ‘SAAB’ was added onto the third place of the list.

#insert 'BMW' onto the second index of the list
#insert 'SAAB' onto the third index of the list
cars.insert(1,'BMW')
cars.insert(2,'SAAB')
#show the results
cars
Out[2]: ['Volvo', 'BMW', 'SAAB','Benz']

2. Remove items of a list

If you want to remove items from a list by its index position, function del() can be used.

The following code shows the first item of the list is removed.

#the first item in the list is removed
del cars[0]

#show the list again
cars
Out[3]: ['BMW', 'SAAB', 'Benz']

And if the items by its value should be removed, you can use function remove(). Note that remove() only applies to the first occurrence in the list. If there are multiple items with the same values to be removed, a for loop combined with remove() might be used instead.

#the item with value 'BMW' is removed
#only the first occurence is applied
cars.remove('BMW')

#show the list again
cars
Out[4]: ['SAAB', 'Benz']

3. Pop elements from a list

Python provides a special function pop() which removes an element from list, and simultaneously make it possible to store the removed element into a new object. Following code show example of this feature.

#create a list 
cars = ['Volvo', 'BMW', 'SAAB', 'Benz']

#pop an item from the list, by default the last item is #popped.
car1 = cars.pop()

#show the list
cars
Out[5]: ['Volvo', 'BMW', 'SAAB']

#the popped item
car1 
Out[6]: 'Benz'

#you can specify which item to be popped.
#here we pop the second item of the list
car2 = cars.pop(1)

#show the list after popped
cars
Out[7]: ['Volvo', 'SAAB']

#the popped item
car2
Out[8]: 'BMW'

You can learn more about Python programming language from our YouTube channel.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago