English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Traversing List Index and Value Methods (Three Methods)

Three methods for traversing the index and value of a list:

Recently, I have been learning the Python programming language and have found that it greatly improves my work efficiency. Especially on Valentine's Day, I wrote this blog post. There will be no more chatter, just the code.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
if __name__ == '__main__':
 list = ['html', 'js', 'css', 'python']
 # Method1
 print 'Traversal list method1:
 for i in list:
 print ("Index:%s Value:%s" % (list.index(i + 1, i))
 print '\nTraversal list method2:
 # Method2
 for i in range(len(list)):
 print ("Index:%s Value:%s" % (i + 1, list[i]))
 # Method3
 print '\nTraversal list method3:
 for i, val in enumerate(list):
 print ("Index:%s Value:%s" % (i + 1, val))
 # Method3
 print '\nTraversal list method3 (Setting the starting position of the traversal, only changing the starting sequence number):'
 for i, val in enumerate(list, 2)
 print ("Index:%s Value:%s" % (i + 1, val))

The result after running the code is shown in the figure below:

Here is an introduction to the enumerate() method, by looking at the help() function, the query results are as follows:

Just a reminder, the second parameter of the enumerate() function only changes the starting value of the sequence, and does not change anything else

That is all for the content of this article. I hope the content of this article can bring certain help to everyone's learning or work, and also hope to give more support to the Shout tutorial!

Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo人工 editing, and does not assume relevant legal liabilities. If you find content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting an issue via email, please replace # with @) for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like