Python - List

      We use arrays in C language to place a large number of data of the same data type one by one, but arrays have a basic requirement, that is, the data types must be the same. We know that Python variables have no data type. That is, Python does not have the concept of arrays, but Python introduces more powerful lists.

       basic concept

       List (list) is a built-in variable sequence in Python, which is an ordered collection of elements;

       Each data in the list is called an element, and all elements of the list are placed in a pair of square brackets "[" and "]" and separated by commas;

       List elements can access individual elements by index.

     

       In Python, the data types of the elements in the same list can be different, and can contain elements of basic types such as integers, real numbers, strings, etc., as well as lists, tuples, dictionaries, sets, functions, and other arbitrary objects.

       Python adopts a value-based automatic memory management mode. Variables do not store values ​​directly, but store references or memory addresses of values. This is also an important reason why variables in python can change types at any time. Similarly, the elements in a Python list are also references to values, so each element in the list can be different types of data.

      An empty list is represented by a single pair of square brackets and no elements.

      There is no limit to the list size and can be modified at any time. When elements are added or deleted from the list, the list object automatically expands or shrinks the memory to ensure that there is no gap between adjacent elements. This automatic memory management function of Python lists can greatly reduce the burden on programmers, but inserting and deleting non-tail elements involves the movement of a large number of elements in the list, which will seriously affect the efficiency.

       When accessing elements in a list, inserting and deleting elements at a non-tail position in index form will change the index of the element after that position in the list, which may lead to unexpected and incorrect results for some operations. Unless absolutely necessary, you should try to append and delete elements from the end of the list.

 

       Create and delete

       As with other types of Python object variables, a list object is created by directly assigning a list to the variable using "=".

       as

1  a_list = ['a', 'b', 'mpilgrim', 'z', 'example']
2  a_list = []           

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325776084&siteId=291194637