# 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]```