SoFunction
Updated on 2025-03-02

Analysis of list operation examples for python development

This article analyzes the list operations of Python development in examples. Share it for your reference, as follows:

For the operation of list in python, you can refer to "Summary of Python list operation usage

Here are my personal notes:

#python list
'''
   There are many ways to create a list:
   1. Create an empty list using a pair of square brackets: []
   2. Use a pair of square brackets, use ','Separate the elements inside:[a, b, c], [a]
   a list comprehension:[x for x in iterable]
   the type constructor:list() or list(iterable)
'''
def create_empty_list():
  '''Using a pair of square brackets to denote the empty list: [].'''
  return []
def create_common_list():
  '''Using square brackets, separating items with commas: [a], [a, b, c].'''
  return ['a', 'b', 'c', 1, 3, 5]
def create_common_list2():
  '''Using a list comprehension: [x for x in iterable].'''
  return [x for x in range(1, 10)]
def str_to_list(s):
  '''Using a string to convert list'''
  if s != None:
    return list(s)
  else:
    return []
def main():
  test_listA = create_empty_list()
  print(test_listA)
  print('#' * 50)
  test_listB = create_common_list()
  print(test_listB)
  print('#' * 50)
  test_listC = create_common_list2()
  print(test_listC)
  print('#' * 50)
  test_str = 'i want to talk about this problem!'
  test_listD = str_to_list(test_str)
  print(test_listD)
if __name__ == '__main__':
  main()

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
[]
##################################################
['a', 'b', 'c', 1, 3, 5]
##################################################
[1, 2, 3, 4, 5, 6, 7, 8, 9]
##################################################
['i', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'l', 'k', ' ', 'a', 'b', 'o', 'u', 't', ' ', 't', 'h', 'i', 's', ' ', 'p', 'r', 'o', 'b', 'l', 'e', 'm', '!']
>>> 

Here are more demos:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> counter = 100
>>> miles = 1000.0
>>> name = "hongten"
>>> numberA,numberB,nameC = 1,2,"Hongten"
>>> list = [counter,miles,name,numberA,numberB,nameC]
>>> print(list)
[100, 1000.0, 'hongten', 1, 2, 'Hongten']
>>> #This is the comment part, the comment starts with "#">>> for element in list:
  print(element)
100
1000.0
hongten
1
2
Hongten
>>> #The above is the list of traversal list>>> print(list[0]) #Get the first element value in the list list100
>>> print(list[-1]) #Get the last element value in the list listHongten
>>> print(len(list)) #Use len(list) to get the length of the list6
>>> num_inc_list = range(10) #Create a list of incremental values>>> print(num_inc_list)
range(0, 10)
>>> for inc_list in num_inc_list:
  print(inc_list)
0
1
2
3
4
5
6
7
8
9
>>> # From here we can see that range(10) produces a numerical increment list starting from 0 to 9>>> initial_value = 10
>>> list_length = 5
>>> myList = [initial_value for i in range(10)]
>>> print(myList)
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> list_length = 2
>>> myList = myList * list_length
>>> print(myList)
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> print(len(myList))
20
>>> #The above is to initialize a list myList with a fixed value initial_value>>> # At the same time, use myList = myList * list_length to copy myList>>> #Let's see the copy effect>>> copyList = [1,2,3,"hongten"]
>>> copyList = copyList * list_length
>>> print(len(copyList))
8
>>> for cl in copyList:
  print(cl)
1
2
3
hongten
1
2
3
hongten
>>> #Low let's take a closer look at the list in python>>> #In a list, it can contain different types of elements, which is similar to arrays in ActionScript 3.0 (AS3.0)>>> test_list = ["hello",1,2,"world",4,5,"hongten"]
>>> print(len(test_list))
7
>>> print(test_list[0]) # Print test_listhello
>>> #Print the first element in test_list>>> print(test_list[-1]) #Print the last element in test_listhongten
>>> print(test_list[-len]) #Print the first elementTraceback (most recent call last):
 File "<pyshell#44>", line 1, in <module>
  print(test_list[-len]) #Print the first elementTypeError: bad operand type for unary -: 'builtin_function_or_method'
>>> print(test_list[-len(test_list)]) #Print the first elementhello
>>> print(test_list[len(test_list) - 1]) #Print the last elementhongten
>>> test_list.append(6) #Add an element to the list>>> print(test_list[-1])
6
>>> test_list.insert(1,0)
>>> print(test_list)
['hello', 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> #The above operation is to insert element 0 into the place where the submark of the list test_list is 1>>> test_list.insert(1,0)
>>> print(test_list)
['hello', 0, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.insert(2,1)
>>> print(test_list)
['hello', 0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> print(test_list.pop(0)) #Return the last element and delete it from test_listhello
>>> print(test_list)
[0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> print(test_list.pop(2)) #The above comment has an error. The pop(index) operation returns the element with the index subscript in the array and deletes it from the list.0
>>> print(test_list)
[0, 1, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.remove(1)
>>> print(test_list)
[0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> #remove(1) means to delete the first occurrence of element 1>>> test_list.insert(0,1)
>>> print(test_list)
[1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.remove(1)
>>> print(test_list)
[0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.insert(2,"hongten")
>>> print(test_list)
[0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.count("hongten")
2
>>> #count(var) is the number of var elements that appear in the list>>> test_list.count("foo")
0
>>> test_list_extend = ["a","b","c"]
>>> test_list.extend(test_list_extend)
>>> print(test_list)
[0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6, 'a', 'b', 'c']
>>> #Use extend(list) to append a list to the source list>>> print(test_list.sort())
Traceback (most recent call last):
 File "<pyshell#76>", line 1, in <module>
  print(test_list.sort())
TypeError: unorderable types: str() < int()
>>> test_list_extend.append("h")
>>> test_lsit_extend.append("e")
Traceback (most recent call last):
 File "<pyshell#78>", line 1, in <module>
  test_lsit_extend.append("e")
NameError: name 'test_lsit_extend' is not defined
>>> list_a = ["e","z","o","r"]
>>> list_a.extend(test_list_extend)
>>> print(list_a)
['e', 'z', 'o', 'r', 'a', 'b', 'c', 'h']
>>> print(list_a.sort()) #Sort list_a listNone
>>> #I don't know why all the above sorts have reported errors...>>> list_b = [1,3,5,2,6,4]
>>> print(list_b.sort())
None
>>> print(sort(list_b))
Traceback (most recent call last):
 File "<pyshell#86>", line 1, in <module>
  print(sort(list_b))
NameError: name 'sort' is not defined
>>> #Not care about sorting issues, let’s take a look at the deletion operation first!  !  !  !  !>>> print(list_b)
[1, 2, 3, 4, 5, 6]
>>> print(del list_b[1])
SyntaxError: invalid syntax
>>> del list_b[1]
>>> print(list_b)
[1, 3, 4, 5, 6]
>>> del list_b[0,2]
Traceback (most recent call last):
 File "<pyshell#92>", line 1, in <module>
  del list_b[0,2]
TypeError: list indices must be integers, not tuple
>>> del list_b[0:2]
>>> print(list_b)
[4, 5, 6]
>>> #del list[index] deletes the element with index subscript, del list[start:end] deletes the element with starting subscript to end subscript>>> del list_b[10]
Traceback (most recent call last):
 File "<pyshell#96>", line 1, in <module>
  del list_b[10]
IndexError: list assignment index out of range
>>> #If the subscript we delete exceeds the length range of the list, an error will be reported!!!>>> ##########################################################################
>>> list_c = range(5);
>>> for c in list_c:
  print(c)
0
1
2
3
4
>>> list_d = list_c
>>> for d in list_d:
  print(d)
0
1
2
3
4
>>> #The above is a copy of the list>>> list_d[2] = 23
Traceback (most recent call last):
 File "<pyshell#108>", line 1, in <module>
  list_d[2] = 23
TypeError: 'range' object does not support item assignment
>>> list_e = [1,2,3,4,5]
>>> list_f = list_e
>>> list_f[2] = 234
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> # From here we can know that list_f copies list_e, list_f is a reference to list_e.>>> #Together they point to an object: [1,2,3,4,5], when our view changes the value of list_f[2],>>> The behavior of the object pointed to by #list_f has changed, that is, the element value has changed, but their references are not>>> #changed.  So it is reasonable to list_e[2] = 234.>>> #######################################################################
>>> list_i = list_e[:]
>>> print(list_i)
[1, 2, 234, 4, 5]
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> list_i[2] = 3
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> print(list_i)
[1, 2, 3, 4, 5]
>>> #The above is a cloning operation of the list, that is, another list is copied. Such an operation will create a new list object>>> # Make list_i and list_e point to different objects, and have different references, so when list_i[2] = 3,>>> #list_e[2] is still equal to 234, that is, it remains unchanged>>>

I hope this article will be helpful to everyone's Python programming.