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 operations on the list in python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian for you to introduce in detail "what is the operation of the list in python", the content is detailed, the steps are clear, the details are handled properly, I hope that this article "what is the operation of the list in python" can help you solve your doubts, the following follows the editor's ideas slowly in depth, together to learn new knowledge.

List1 = ['ham sausage', 'bread', 'milk', 'cola', 'instant noodles'] 1. Add # append: add the element list1.append ('biscuit') print (list1) to the end of the list

# results: ['ham sausage', 'bread', 'milk', 'Coke', 'instant noodles', 'biscuits']

# insert: insert an element list1.insert (1) print (list1) under an index

# results: ['ham sausage', 'steamed buns', 'bread', 'milk', 'cola', 'instant noodles']

# extend: concatenate two lists list2= [1, list2 2] list1.extend (list2) print (list1)

# results: ['ham sausage', 'bread', 'milk', 'cola', 'instant noodles', 1, 2]

two。 Delete # remove: delete element list1.remove ('milk') print (list1) # result: ['ham sausage', 'bread', 'Coke', 'instant noodles'] # remove: delete element list1.remove ('milk') print (list1) by element value

# results: ['ham sausage', 'bread', 'cola', 'instant noodles']

# pop: delete the element through the index. If the index is not filled, the last one is deleted by default. Pop can return deleted elements to list1.pop (- 2) print (list1.pop ()) print (list1)

# results:

# Instant Noodles

# ['ham sausage', 'bread', 'milk']

# del: delete a single element (function is the same as pop), delete multiple elements Delete the entire list from memory del list1 [- 2] print (list1) # result: ['ham sausage', 'bread', 'milk', 'instant noodle'] del list1 [1:3] print (list1) # result: ['ham sausage', 'Coke', 'instant noodle'] # all variable names are deleted from memory del list1# clear: clear the list, the list is still there But each content in it print (list1.clear ()) print (list1)

# results:

# None

# []

3. Find / modify # index: find the index of an element to make it easier to modify the element at that location print (list1.index ('milk')) list1 [list1.index ('milk')] = 'banana milk' print (list1)

# results:

# 2

# ['ham sausage', 'bread', 'banana milk', 'cola', 'instant noodles']

# count: get the number of occurrences of an element in the list. A return value of 0 indicates that there is no element print (list1.count ('milk')) # result: 1print (list1.count ('orange juice')) # result: 04. Other # in and not in: determine whether an element is in the list print ('bread' in list1) print ('bread' not in list1)

# results:

# True

# False

# reverse: inverts the elements in the list, and the last element becomes the first list1.reverse () print (list1)

# results: ['instant noodles', 'coke', 'milk', 'bread', 'ham sausage']

# sort: sort by default, list2= in ascending order for numbers [2 print 4 list2.sort 1 list2 1 list2.sort 9 22 11 8] print (list2) list2.sort (reverse=True) print (list2)

# results:

# [- 1,1,2,4,8,9,11,22]

# [22, 11, 9, 8, 4, 2, 1,-1]

# copy: copy a new list list2 = list1.copy () print (list2). This article "what is the operation of the list in python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, please follow the industry information channel.

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