# Manipulating list elements in Python

By Abhinash & Priya Chetty on August 23, 2022

So far, we have seen that a string object in Python is immutable. However, lists, although being sequence data, are mutable. A major form of manipulating the list elements in Python is by either changing items or by adding new items to a list.

## Sorting list elements

Since Python lists are unordered groups of elements, sometimes they are needed to be ordered in ascending or descending order. This can be done in Python using either the list.sort(reverse=false) or sorted(list,reverse=false), which arranges the items in ascending order. While list.sort( ) does not return any value but updates the list. Whereas, the sorted( ) returns the updated list.

``````a = [9, 3, 15, 30, 27, 12]
b = a.sort()
print(a,b)``````
```#OUTPUT
[3, 9, 12, 15, 27, 30] None```
``````a = [9, 3, 15, 30, 27, 12]
b = sorted(a)
print(a,b)``````
```#OUTPUT
[3, 9, 12, 15, 27, 30] [3, 9, 12, 15, 27, 30]```

By setting the reverse argument to True or by using list.reverse( ) or reverse(list) the list can be arranged in descending order.

``````a = [9, 3, 15, 30, 27, 12]
a.sort(reverse=True)
print(a)``````
```#OUTPUT
[30, 27, 15, 12, 9, 3]```
``````a = [9, 3, 15, 30, 27, 12]
a.reverse()
print(a)``````
```#OUTPUT
[30, 27, 15, 12, 9, 3]```

## Manipulating list elements by replacing a specific item

Manipulating a list element by replacing it with a new item by identifying the index of the item in the list.

``````cake = ['flour', 'eggs', 'sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence']
#Determining index of sugar
i = cake.index('sugar')
#Replacing sugar with Brown sugar
cake[i] = 'Brown sugar'
print(cake)``````
```#OUTPUT
['flour', 'eggs', 'Brown sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence']```

## Manipulating list elements by adding new items with append()

Appending refers to the addition of a new element to the end of the list. This can be done by list.append(newItem) in-built function.

``````cake = ['flour', 'eggs', 'sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence']
cake.append('cocoa powder')
print(cake)``````
```#OUTPUT
['flour', 'eggs', 'sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence', 'cocoa powder']```

Similarly, a new list of items can also be added to an existing list.

``````cake = ['flour', 'eggs', 'sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence']
cake.append(['cocoa powder','apple juice'])
print(cake)``````
```#OUTPUT
['flour', 'eggs', 'sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence', ['cocoa powder', 'apple juice']]```

## Inserting an item to a specific position in a list

New items can be added to an existing list at specific positions with list.insert().

``````cake = ['flour', 'eggs', 'sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence']
cake.insert(1,'cocoa powder')
print(cake)``````
```#OUTPUT
['flour', 'cocoa powder', 'eggs', 'sugar', 'butter', 'baking powder', 'baking soda', 'vanilla essence']```

## Merging two different lists with extend()

list.extend(newList) combines ‘newList’ to ‘list’.

``````a = [ 9, 3, 15, 30]
b = [12, 24, 6, 18]
a.extend(b)
print(a)``````
```#OUTPUT
[9, 3, 15, 30, 12, 24, 6, 18]```

I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them.

Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here

I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal.

I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.