SoFunction
Updated on 2024-11-15

Simple Examples of Common Operations on Python List Elements

This article example describes the common operations of Python list elements. Shared for your reference, as follows:

A list is similar to an array in java, with square brackets and commas separating the elements.

#Assign, print
children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
print(children_names)

Run results:

['Du Ziteng', 'Du Xiaoyue', 'Du Xiaoxing', 'Du Xiaoyang', 'Du Xiaohua']

Access to one of the elements

children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
print(children_names[2])   # Print one of the elements by index, starting at 0
print(children_names[-1])  # Print the last element, by index, and so on -1,-2,-3...
print(len(children_names))  # Get the length of the list

Run results:

Du Xiaoxing (1905-1992), Mao *'s second wife
Dulcimer (Harry Potter)
5

Modifying elements

children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
children_names[2]='Du Xiaolao'  # Follow the index and directly override the assignment
print(children_names)

Run results:

['Du Ziteng', 'Du Xiaoyue', 'Du Xiaolao', 'Du Xiaoyang', 'Du Xiaohua']

Adding Elements

children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
children_names.append("Du Xiaolai 2.")  #Append to the end of the list
children_names.insert(0,"Du Xiaodu.")   # Insert elements according to index position
print(children_names)

Run results:

['Du Xiaodu', 'Du Ziteng', 'Du Xiaoyue', 'Du Xiaoxing', 'Du Xiaoyang', 'Du Xiaohua', 'Du Xiaolao 2']

Delete element

  • The difference between the use of del and pop is that after deletion it is used or not [based on the index].
  • Delete by value, remove
del children_names[0]  # Delete elements completely, by index
children_pop = children_names.pop()
# To be precise, pop up the tail element of the list [you can also specify the index], assign it to a variable, and save it temporarily
children_names.remove("Du Xiaolai 2.") # If there are duplicates, only the first one will be deleted.

Sorting the list

  • Use sort to sort permanently in alphabetical order
  • Temporarily sort the list in alphabetical order using sorted
  • Print the list backwards
visitors = ['a1','b1','c1','d1','e']
() # Alphabetical, sorted, irreversible #
(reverse=True) # In reverse alphabetical order, irreversible
print(sorted(visitors)) # Temporary sorting without affecting the existing data order
print(sorted(visitors,reverse=True)) # Temporary reverse sorting without affecting the existing data order
()  # Directly inverted, not related to alphabetical order, reversible, just do it again #

Run results:

['a1', 'b1', 'c1', 'd1', 'e']
['e', 'd1', 'c1', 'b1', 'a1']

More about Python related content can be viewed on this site's topic: thePython list (list) manipulation techniques summarized》、《Summary of Python string manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.