Home >Backend Development >Python Tutorial >How to use the enumerate() function in python

How to use the enumerate() function in python

高洛峰
高洛峰Original
2017-03-21 13:10:372101browse

When processing various types of sequences in python, if we want to display the elements of this sequence and their subscripts , we can use enumerate()function.

enumerate() function is used to traverse the elements in the sequence and their subscripts. The usage is as follows:

1. The parameter is a tuple:

for index, value in enumerate(('a', 'b', 'c')):
    '''下标,元素'''
    print(index,value)

2..The parameter is a listlist:

for index, value in enumerate([1, 2, 3]):
    '''下标,元素'''
    print(index, value)

3.The parameter is a stringstring:

for index, value in enumerate('abc'):
    '''下标,元素'''
    print(index, value)

4. The parameter is a dictionary dict:

for index, value in enumerate({'a': 1, 'b': 2}):
    '''下标(字典无序性),元素(Key值)'''
    print(index, value)

The above is the detailed content of How to use the enumerate() function in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn