Sitemap

[Python] Modified List

4 min readAug 27, 2024

--

There are currently many programming languages in use, and one of the most popular is Python. Today, we will be discussing how to modify lists.

In Python programming, arrays are referred to as Lists. In other programming languages, Lists are known as Arrays. An array is a collection of data in the form of Lists, Integers, Strings, Tuples, and more. Lists can handle a wide variety of data types.

In this session, we will provide examples of several methods to modify Lists. For instance, methods such as append(), extend(), insert(), and etc. First, let's create a List using the following example:

a = [1,2,3,4,5,6]
b = []
c = ['a','b',44,98,[39,55]]

append()

The append() method is used to add data or values to a List. This method automatically adds the element to the end of the List. It has some similarities to the insert() method.

a.append(15)
print(a) # [1, 2, 3, 4, 5, 6, 15]
b.append(20)
print(b) # [20]

extend()

The extend() method is used to combine two lists that have already been declared. This method works by taking all the elements from the second list and adding them one by one to the end of the first list. Unlike the append() method, which adds a single element at a time, extend() effectively merges the second list into the first, expanding the original list with the new elements in sequential order. As a result, the first list grows in size, incorporating all the elements from the second list while maintaining the original order of both lists.

x = [1,2,3,4,5,6]
y = [5,6,7,8,9,10]
x.extend(y)
print(x) # [1, 2, 3, 4, 5, 6, 5, 6, 7, 8, 9, 10]

insert()

Get Andhi YP’s stories in your inbox

Join Medium for free to get updates from this writer.

This method is used to add a value to a list. The value added to the list can be an integer, string, another list, tuple, or other data types. The insert() method takes two parameters : the position index and the value to be inserted. The position index starts at 0.

a.insert(0,100)
print(a) # [100, 1, 2, 3, 4, 5, 6, 15]

a.insert(0, 'Budi')
print(a) # ['Budi', 100, 1, 2, 3, 4, 5, 6, 15]

remove()

This method is used to remove a specific value from a list. Unlike methods that rely on the index or position of the element, this method takes the value itself as the parameter and removes the first occurrence of that value from the list. If the value exists multiple times in the list, only the first instance will be removed, and the rest will remain unchanged. If the value is not found in the list, the method will raise an error. This approach is useful when you want to remove a specific item but don’t know or don’t want to reference its index directly.

a.remove('Budi')
print(a) # [100, 1, 2, 3, 4, 5, 6, 15]

a.remove(15)
print(a) # [100, 1, 2, 3, 4, 5, 6]

sort()

This method is used to sort the values within a list. It provides flexibility in sorting order, allowing for either ascending or descending arrangements based on your needs. To sort a list in ascending order, the method will arrange the elements from the smallest to the largest. Conversely, for descending order, it will arrange the elements from the largest to the smallest.

However, this method has a key limitation: it requires all elements in the list to be of the same data type. If the list contains a mixture of different data types, such as integers and strings, sorting cannot be performed and will result in an error. This is because the method relies on comparing elements of a uniform type to establish the correct order. Therefore, ensure that your list contains only elements of the same type before attempting to sort it.

a = [2,4,3,88,5,7,'Budi',99,1]
a.sort()
print(a) # error.....

a = [2,4,3,88,5,7,99,1]
a.sort()
print(a) # [1, 2, 3, 4, 5, 7, 88, 99]

a.sort(reverse = True)
print(a) # [99, 88, 7, 5, 4, 3, 2, 1]

copy()

This method is used to copy the data from one list into a new list. It essentially performs a copy-and-paste operation. When you use this method, the entire content of the original list is duplicated into a new list, creating a separate instance.

Once the data has been copied to the new list, there is no further connection or linkage between the original list and the new one. This means that changes made to the original list after the copy operation will not affect the new list, and vice versa. Each list operates independently, allowing you to modify, add, or remove elements in one list without impacting the other. This method is useful for creating a duplicate list where you need to work with the same set of data without altering the original dataset.

a = [20,30,40,99]
b = a.copy()
a.append('Budi')
print(a) # [20, 30, 40, 99, 'Budi']
print(b) # [20, 30, 40, 99]

--

--