Manipulating list elements in Python
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 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 an interdisciplinary educator, researcher, and technologist with over a decade of experience in applied coding, educational design, and research mentorship in fields spanning management, marketing, behavioral science, machine learning, and natural language processing. I specialize in simplifying complex topics such as sentiment analysis, adaptive assessments and data visualizatiion. My training approach emphasizes real-world application, clear interpretation of results and the integration of data mining, processing, and modeling techniques to drive informed strategies across academic and industry domains.
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.
Discuss