Built-in practice: enumerate()
Dec 31, 2023
names = ['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
# Method I
# Rewrite the for loop to use enumerate
indexed_names = []
for i,name in enumerate(names):
index_name = (i,name)
indexed_names.append(index_name)
print(indexed_names)
# Method II
# Rewrite the above for loop using list comprehension
indexed_names_comp = [(i,name) for i,name in enumerate(names)]
print(indexed_names_comp)
# Method III
# Unpack an enumerate object with a starting index of zero
indexed_names_unpack = [*enumerate(names, start=0)]
print(indexed_names_unpack)
# output
[(0, 'Jerry'), (1, 'Kramer'), (2, 'Elaine'), (3, 'George'), (4, 'Newman')]
![[2022] A survey on recommendation systems](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*cKGWQcsoCn_CRvZ4zGrlFg.png)





