enumerate in python example
Example 1: enumerate in python
languages = ['Python', 'C', 'C++', 'C#', 'Java']
i = 0
for language in languages:
print(i, language)
i+=1
for i, language in enumerate(languages):
print(i, language)
Example 2: enumerate python
for index,char in enumerate("abcdef"):
print("{}-->{}".format(index,char))
0-->a
1-->b
2-->c
3-->d
4-->e
5-->f
Example 3: enumerate in python
list1 = ['1', '2', '3', '4']
for index, listElement in enumerate(list1):
print(f'{listElement} is at index {index}')
"""
1 is at index 0
2 is at index 1
3 is at index 2
4 is at index 3
"""
Example 4: enumerate
categorical_features_new=[features for features in df.columns if df[features].dtypes=='object']
for feature in categorical_features_new:
labels_sorted=df[feature].value_counts().sort_values().index
labels_ordered={k:i for i,k in enumerate(labels_sorted,0)}
df[feature]=df[feature].map(labels_ordered)