Home  >  Article  >  Backend Development  >  What does list mean in python

What does list mean in python

silencement
silencementOriginal
2019-06-21 16:00:4337610browse

What does list mean in python

List in python is a list, which is a data type.

Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, with the first index being 0, the second index being 1, and so on.

Python has 6 built-in types for sequences, but the most common are lists and tuples.

Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.

List is the most commonly used Python data type, which can appear as a comma-separated value within square brackets.

The data items of the list do not need to be of the same type

To create a list, just use square brackets to enclose the different data items separated by commas. It looks like this:

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]

Like string indexing, list indexing starts at 0. Lists can be intercepted, combined, etc.

Add elements to the list

>>> li 
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li.append("new")
>>> li 
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
>>> li.insert(2, "new")
>>> li 
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
>>> li.extend(["two", "elements"]) 
>>> li 
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']

Traverse the list

list1 = [x for x in range(0,10,2)]         
 # 方法一      
 for i in range(len(list1)):         
      print(list1[i], end=' ')           
 # 方法二      
 for x in list1:       
    print(x, end=' ')           
 # 方法三      
 for ind,value in enumerate(list1):          
      print(ind, value, sep='=', end = ' ')

The above is the detailed content of What does list mean in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn