List Operation Method summary in Python, pythonlist

Source: Internet
Author: User

List Operation Method summary in Python, pythonlist

The examples in this article summarize the common operations on the list in Python for your reference. The specific method is as follows:

I. Python creation list:

sample_list = ['a',1,('a','b')]

Ii. Python list operations:

Suppose there is a list as follows:

sample_list = ['a','b',0,1,3]

1. Get a value in the list:

value_start = sample_list[0]end_value = sample_list[-1]

2. Delete the first value of the list:

del sample_list[0]

3. Insert a value to the list:

sample_list[0:0] = ['sample value']

4. Get the length of the list:

list_length = len(sample_list)

5. List traversal:

for element in sample_list:  print(element)

Iii. Python list Advanced Operations/skills

1. generate a list of incremental values:

num_inc_list = range(30)#will return a list [0,1,2,...,29]

2. initialize the list with a fixed value:

initial_value = 0list_length = 5sample_list = [ initial_value for i in range(10)]sample_list = [initial_value]*list_length# sample_list ==[0,0,0,0,0]

On this basis, readers can continue to collect other methods for Python list operations to further consolidate and deepen their understanding of Python list operations.


Python list operations

Direct:
New_list = [I for I in a_list if I> 0]
New_list is a number greater than 0.

Python list Operation

Let me answer.

For index in range (len (list )):
List [index] = int (10 * list [index])
Print list

# Now you can see your satisfactory results. I tried it. No problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.