Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the operation methods of Python list

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail about the operation of the Python list, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

1. Add elements to the List method 1.1 the Python append () method adds elements

The append () method is used to append elements to the end of the list. The syntax format of this method is as follows:

Listname.append (obj)

Where listname represents the list of elements to be added; obj represents the data added to the end of the list, which can be a single element, list, tuple, and so on.

1.2 the Python extend () method adds elements

The difference between extend () and append () is that instead of treating a list or meta-ancestor as a whole, extend () adds the elements they contain to the list one by one.

The syntax format of the extend () method is as follows:

Listname.extend (obj)

Where listname refers to the list of elements to be added; obj represents the data added to the end of the list, which can be a single element, list, tuple, and so on.

Example:

L = ['Python',' Centrum, 'Java'] # append element l.extend (' C') print (l) # append tuple, meta-ancestor is split into multiple elements t = ('JavaScript',' Centrum, 'Go') l.extend (t) print (l) # append list, and the list is also split into multiple elements l.extend ([' Ruby', 'SQL']) print (l)

Running result:

['Python',' Centrum', 'Java',' C']

['Python',' Clearing, 'Java',' Clearing, 'JavaScript',' Clearing, 'Go']

['Python',' Ruby', 'SQL',' Java', 'Centrum,' JavaScript', 'Centrum,' Go', 'Cruise]

1.3 the Python insert () method inserts an element

The append () and extend () methods can only insert elements at the end of the list, and if you want to insert elements somewhere in the middle of the list, you can use the insert () method.

The syntax format of insert () is as follows:

Listname.insert (index,obj)

Where index represents the index value of the specified location. Insert () inserts obj into the location of the index element of the listname list. When inserting lists or meta-ancestors, insert () also inserts them as a whole and into the list as an element, just like append ().

2. The method of deleting elements from List

Deleting elements in the Python list can be divided into the following three scenarios:

Delete according to the index of the location of the target element, you can use the del keyword or the pop () method

To delete based on the value of the element itself, you can use the remove () method provided by the list (list type)

To delete all elements in the list, use the clear () method provided by the list (of type list).

2.1 del: delete elements based on index values

Del can delete individual elements from the list in the format:

Del listname [index]

Where listname represents the list name and index represents the index value of the element. Del can also delete consecutive elements in the middle segment.

The format is:

Del listname [start: end]

2.2 pop (): deletes elements based on index values

The Python pop () method is used to delete the element at the specified index in the list.

The specific format is as follows:

Listname.pop (index)

Where listname represents the list name and index represents the index value. If the index parameter is not written, the last element in the list is deleted by default, similar to the "out-of-stack" operation in the data structure.

Most programming languages provide a method corresponding to pop (), which is used to add elements to the end of the list, similar to the "stack" operation in a data structure.

However, Python is an exception, and Python does not provide a push () method, because you can use append () instead of push ().

2.3 remove (): delete based on element value

In addition to the del keyword, Python also provides the remove () method, which deletes based on the value of the element itself.

It is important to note that the remove () method only deletes the first element that is the same as the specified value, and must ensure that the element exists, otherwise a ValueError error will be thrown.

Nums = [40, 36, 89, 2, 36, 100, 7] # first deletion of 36nums.remove (36) print (nums) # second deletion of 36nums.remove (36) print (nums) # deletion of 78nums.remove (78) print (nums)

Running result:

[40, 89, 2, 36, 100, 7]

[40, 89, 2, 100, 7]

Traceback (most recent call last):

File "C:\ Users\ mozhiyan\ Desktop\ demo.py", line 9, in

Nums.remove (78)

ValueError: list.remove (x): x not in list

For the last deletion, since 78 does not cause an error, we'd better judge in advance when we delete the element using remove ().

2.4 clear (): delete all elements of the list

Python clear () is used to delete all elements of the list, that is, to empty the list

Take a look at the following code:

Url = list ("http://c.biancheng.net/python/")url.clear()print(url)

Running result:

[]

3. List list modification element 3.1 modify a single element

Modifying a single element is as simple as assigning a value to the element directly.

Take a look at the following example:

Nums = [40, 36, 89, 2, 36, 100, 7] nums [2] =-26 # use positive index nums [- 3] =-66.2 # use negative index print (nums)

Running result:

[40, 36,-26, 2,-66.2, 100, 7]

After you get the list element using the index, you change the value of the element by assigning a = value.

3.2 modify a set of elements

Python supports assigning values to a set of elements through slicing syntax. When doing this, if you do not specify a step size (the step parameter), Python does not require that the number of newly assigned elements be the same as the original; this means that this operation can either add elements to the list or delete elements from the list.

Example:

Nums = [40, 36, 89, 2, 36, 100, 7] # modify the value of element 1 / 4 (excluding element 4) nums [1: 4] = [45.25,-77,-52.5] print (nums)

Running result:

[40, 45.25,-77,-52.5, 36, 100, 7]

4. List list lookup element 4.1 index () method

The index () method is used to find where an element appears in the list (that is, the index). If the element does not exist, it will cause a ValueError error, so it is best to use the count () method to determine before looking for it.

The syntax format of index () is:

Listname.index (obj,start,end)

Where listname represents the name of the list, obj represents the element to be found, start represents the start position, and end represents the end position.

4.2 count () method

The count () method is used to count the number of times an element appears in the list

The basic syntax format is:

Listname.count (obj)

Where listname represents the list name and obj represents the element to be counted.

If count () returns 0, it means that the element does not exist in the list, so count () can also be used to determine whether an element in the list exists.

This is the end of this article on "how to operate the Python list". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report