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

How to use the enumerate() function in python

高洛峰
Release: 2017-03-21 13:10:37
Original
2079 people have browsed it

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)
Copy after login

2..The parameter is a listlist:

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

3.The parameter is a stringstring:

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

4. The parameter is a dictionary dict:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template