Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The usage of for Loop and list in python

Shulou Source: shulou.com Published: 2022-06-02 09:35:22 09月20日 Update

This article focuses on "the use of for loop and list in python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of for loop and list in python.

List = [1,2,3,4,5,]

Modify the value:

List [2] = 500

Print (list)

[1, 2, 500, 4, 5]

Value according to the index:

For index in range (len (list)):

Print (ListIndex)

one

two

five hundred

four

five

Direct value:

For item in list:

Print (item)

one

two

five hundred

four

five

Index and value are obtained at the same time:

For index, item in enumerate (list):

Print (index, item)

0 1

1 2

2 500

3 4

4 5

Additional

List.append (7)

Print (list)

[1, 2, 500, 4, 5, 7]

Add to specified location

List.insert (1,9)

Print (list)

[1, 9, 2, 500, 4, 5, 7]

List merger

List.extend ([222333])

Print (list)

[1, 9, 2, 500, 4, 5, 7, 222, 333]

List deletion

If 2 in list: list.remove (2)

Print (list)

[1, 9, 500, 4, 5, 7, 222, 333]

Pop operation

List.pop ()

333 # removed value

Print (list)

[1, 9, 500, 4, 5, 7, 222]

Specify index removal

List.pop (0)

1 # removed value

Print

Print (list)

[9, 500, 4, 5, 7, 222]

Clear the list

List.clear ()

Print (list)

[]

At this point, I believe you have a deeper understanding of "the use of for loop and list in python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Looping indexing content learning practical deeper location interest at the same time practical practical easy to operate methods more friends websites channels queries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux Docker Apple Shulou Technology