Last active
March 3, 2024 16:59
-
-
Save TGITS/67d64ec250392e22a8cb3ef5b5142103 to your computer and use it in GitHub Desktop.
Exemple d'utilisation de enumerate en Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] | |
print('letters {}'.format(letters)) | |
print('\n##########\n') | |
print('In Python, the for is a foreach : ') | |
for letter in letters: | |
print(letter) | |
print('\n##########\n') | |
# Naive approach to emulate a C-like for loop in Python | |
print('Naive approach to access both index of element in the list and the associated value : ') | |
for index in range(len(letters)): | |
print('letters[{}] : {}'.format(index, letters[index])) | |
print('\n##########\n') | |
# For loop with enumerate | |
print('using enumerate in a for loop : ') | |
for index, value in enumerate(letters): | |
print('letters[{}] : {}'.format(index, value)) | |
print('\n##########\n') | |
# For comprehension with enumerate | |
print('using enumerate in a for comprehension : ') | |
list_to_print = ['letters[{}] : {}'.format( | |
index, value) for index, value in enumerate(letters)] | |
print('\n'.join(list_to_print)) | |
print('\n##########\n') | |
# For loop with enumerate, ignoring a value | |
print('using enumerate in a for loop and ignoring a value : ') | |
for index, _ in enumerate(letters): | |
print('index of letter :', index) | |
print('\n##########\n') | |
# Creating a list of tuples from enumerate | |
print('Creating a list of tuples (index, value) from a list of values') | |
letters_with_index = list(enumerate(letters)) | |
print(letters_with_index) | |
print('\n##########\n') | |
# enumerate works with tuple, string and set for example | |
print('enumerate with tuple : ') | |
print(list(enumerate(('a', 'e', 'i', 'o', 'u')))) | |
print('enumerate with string : ') | |
print(list(enumerate(('aeiou')))) | |
print('enumerate with set : ') | |
print(list(enumerate({'a', 'e', 'i', 'o', 'u', 'a', 'e'}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment