I'm trying to generate a unique index list with a for loop and a condition. It seems that because my original list has duplicate values, it's only returning the first found index multiple times.
my list: ['Section', 'FINANCE Cost Category', 'Major Tasks', 'Sub Tasks', 'Start Date', 'End Date', 'Resource Code', 'Resource', 'Reporting Region', 'Rate', 'Effort', nan, 'Unit of Measure (UOM)', 'Invoice Trigger', 'Unit Hours', 'Total FTE', 'Unit Cost', '# of Units ', 'Extended Hours', 'Budget Total', '# of Units ', 'Extended Hours', 'Budget Total', nan, '# of Units ', 'Extended Hours', 'Budget Total', nan, '# of Units ', 'Extended Hours', 'Budget Total', nan, '# of Units ', 'Extended Hours', 'Budget Total', nan, '# of Units ', 'Extended Hours', 'Budget Total', nan, '# of Units ', 'Extended Hours', 'Budget Total', nan, '# of Units ', 'Unit Hours', 'Unit Cost', 'Extended Hours', 'Budget Total', 'Service Provider Comments', nan, nan, nan, nan, nan, nan, nan]
My block:
header_mod_list = []
for each in headers:
if 'budget' in str(each).lower():
header_mod_list.append(headers.index(each))
print(header_mod_list)
This is yielding:
[19, 19, 19, 19, 19, 19, 19, 19]
What I want:
[19, 22, 26, 30, 34, 38, 42, 48]
Any thoughts?
Thank you!
CodePudding user response:
Use enumerate:
header_mod_list = []
for i, each in enumerate(headers):
if 'budget' in str(each).lower():
header_mod_list.append(i)
print(header_mod_list)