Home  >  Article  >  Backend Development  >  How to use lists in python

How to use lists in python

coldplay.xixi
coldplay.xixiOriginal
2020-10-30 14:35:1141712browse

How to use lists in Python: 1. To access the values ​​in the list, use subscript index to access the values ​​in the list; 2. Update the list and use the [append()] method to add list items; 3. To delete elements from a list, you can use the del statement to delete elements from the list.

How to use lists in python

How python uses lists:

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 separate the different data items with commas using square brackets Just enclose it. 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.

Accessing values ​​in the list

Use subscript index to access the value in the list. You can also use square brackets to intercept characters, as shown below:

Example (Python 2.0)

#!/usr/bin/python
 
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
 
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

Output result of the above example:

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

Update list

You can update the data of the list To modify or update an item, you can also use the append() method to add a list item, as shown below:

Example (Python 2.0)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
list = []          ## 空列表
list.append('Google')   ## 使用 append() 添加元素
list.append('Runoob')
print list

Note: We will discuss this in the next chapter Discuss the use of the append() method

Output results of the above examples:

['Google', 'Runoob']

Delete list elements

You can use the del statement to delete elements of the list, The following example:

Example (Python 2.0)

#!/usr/bin/python
 
list1 = ['physics', 'chemistry', 1997, 2000]
 
print list1
del list1[2]
print "After deleting value at index 2 : "
print list1

The output result of the above example:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

Related free learning recommendations: python video tutorial

The above is the detailed content of How to use lists 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