Python Data Structures – List

Python Data Structures - List - Pop - Remove - Extend - Insert - Extend - Functions and its usage. Sorting a list - Immutable List

List

List is a collection of values.

Properties of List are,

🍁 Unordered by default

🍁 Sorting can be achieved using in-built function

🍁 Duplicates are allowed

🍁 Heterogeneous – multi type values are acceptable i.e. Allow all data types of values in a single list itself

Lists are completely mutable:

Mutable Elements: A particular element can be changed by using its index.

Mutable list: A list structure (Size) can be modified. i.e., A list can grow or shorten using the in-built functions pop, remove, append & extend.

list1 = [2,10,"test",54,20]
print(list1)
print(type(list1))

Output:

[2, 10, 'test', 54, 20]
<class 'list'>

Multi-dimensional list

listing = [[1,2],[3,4],['a',['er',345]]]
print(listing[1][1])
print(listing[1][0])
print(listing[2][1][1])

Output:

4
3
345

List Slicing

listname[start:stop:step]

start will be included, stop will be excluded

Default start is 0, if not provided it will be 0

Default stop is end of list

We can create a new list using the subset/members of the original list.

list1 = [10, 8, 4, 10, 4, 5]
print(list1[:])
print(list1[::2])
print(list1[1:3])
[10, 8, 4, 10, 4, 5]
[10, 4, 4]
[8, 4]

In the first print statement “list1[:]”, there is no elements in start and stop option. Which means The selection will start from initial index and stop at the end of the list. Thus it returns all the elements.

In the statement “list1[::2]”, the third slicing option “2” means that the selection start from 0 and 2 elements will be skipped.

And then “4” is returned. Again 2 elements skipped. Then next element “4” is returned.

Thus the result is [10, 4, 4]

In the third print statement, the slicing starts from index 1, i.e., second element (first element’s index is 0) and the slicing stopped at index 3 which is 10. Also index 3 is excluded.

Negative List Indexing:

Using negative number (negative list indexing) as step will start slicing in reverse order.

I.e., the Slicing will start from last position and move towards the first position.

list1 = [10, 8, 4, 10, 4, 5]
print(list1[::-2])
print(list1[::-1])
[5, 10, 8]
[5, 4, 10, 4, 8, 10]

Mutable elements:

A single element of a list can be accessed and modified using its index.

list1 = [10, 8, 4, 'test', 4, 5]
print("List Before change:",list1)
print(list1[3])
list1[3] = 15
print("List After change:",list1)

Output:

List Before change: [10, 8, 4, 'test', 4, 5]
test
List After change: [10, 8, 4, 15, 4, 5]
print(list1[2])
list1[2] = 4
print(list1)

Output:

test
[2, 10, 4, 54, 20]

Mutable list

The order of the elements can be changed by inserting an element into any index or removing an element from anywhere in the list.

A list can be grown by appending it with an element using append() function.

For bulk addition the extend() function can be used to join another list at the end of the list.

Pop:

The Pop function takes an numeric argument as index. Removes the element at index and returns the removed item.

If the no argument is given, then it remove and returns the last element by default.

print(list1.pop())
print(list1)
print(list1.pop(2))
print(list1)

Output:

20
[2, 10, 4, 54]
4
[2, 10, 54]

If the given index is not existing in list, then the IndexError will be raised.

list4.pop(15)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-30-d21fe070190f> in <module>
----> 1 list4.pop(15)

IndexError: pop index out of range

Remove:

Remove function takes an element value as argument and removes it from list.

If the value exists in more than 1 index, then the first occurrence will be removed.

list1 = [10, 4, 8, 99, 10, 4, 5]
list1.remove(10) 
print(list1)
[4, 8, 99, 10, 4, 5]

The value 10 in first index is removed.

If the given value is not existing in list, ValueError will be raised.

list1.remove(3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-8b72e871f99a> in <module>
----> 1 list1.remove(3)

ValueError: list.remove(x): x not in list
insert vs append vs extend

Insert:

The insert function can be used to inject a value in a specified location.

list1 = [10, 4, 8, 99, 5]
print("List Before change:",list1)
list1.insert(2,11)
print("List After change:",list1)
List Before change: [10, 4, 8, 99, 5]
List After change: [10, 4, 11, 8, 99, 5]

Append:

Append function is used to grow a list but it can add only 1 element.

list1 = [10, 4, 8, 99, 5]
print("List Before change:",list1)
list1.append(100)
print("List After change:",list1)
List Before change: [10, 4, 8, 99, 5]
List After change: [10, 4, 8, 99, 5, 100]

Even if the function is given with a list as argument, the entire list will be added as a last element and makes the list to multi-dimensional.

list1 = [10, 4, 8, 99, 5]
print("List Before change:",list1)
list2 = [22, 11]
list1.append(list2)
print("List After change:",list1)
print("Last element:",list1[5])
print("Length of last element:",len(list1[5]))
List Before change: [10, 4, 8, 99, 5]
List After change: [10, 4, 8, 99, 5, [22, 11]]
Last element: [22, 11]
Length of last element: 2

Extend:

To grow a list in bulk manner, the function extend() can be used.

list1 = [10, 4, 8, 99, 5]
print("List Before change:",list1)
list2 = [22, 11]
list1.extend(list2)
print("List After change:",list1)
print("New elements:",list1[5], list1[6])
List Before change: [10, 4, 8, 99, 5]
List After change: [10, 4, 8, 99, 5, 22, 11]
New elements: 22 11
AppendVsExtend

Sorting:

Sort function takes in a Boolean argument to specify whether the sorting should be done in ascending or descending order.

list1 = [10, 8, 4, 10, 4, 5]
list1.sort(reverse = False)
print("Ascending Order:", list1)
list1.sort(reverse = True)
print("Descending Order:",list1)
Ascending Order: [4, 4, 5, 8, 10, 10]
Descending Order: [10, 10, 8, 5, 4, 4]
Descending Order: [4, 4, 5, 8, 10, 10]

The default value of the reverse argument is False. i.e., ascending order.

The sorting function will not return any values. It just sorts the list itself.


Asha Ponraj
Asha Ponraj

Data science and Machine Learning enthusiast | Software Developer | Blog Writter

Articles: 86

Leave a Reply

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