'How to check next element in a for loop based off of a condition? [closed]

So I am currently stuck on trying to iterate over my for loop while checking the elements based off of my condition:

Let's say I have this dictionary:

testing_dic = {
    "1800": [
        "Human Capital",
        "The sustainability of Abbott’s business depends on attracting, engaging and developing talented people with diverse backgrounds who share Abbott’s mission to help people live their healthiest possible lives. Abbott provides its employees opportunities to grow and develop their careers, market competitive compensation and benefit programs, and the satisfaction of being part of a global company dedicated to improving health in more than 160 countries.",
        "4",
        "As of December 31, 2020, Abbott employed approximately 109,000 people, 70% of whom were employed outside of the U.S. Women represented 47% of Abbott’s U.S. workforce, 45% of its global workforce, and 39% of its managers.",
        "Health and Safety",
        "The health, safety and wellness of its employees is an Abbott priority embedded at every level of its business.  Abbott’s integrated Environmental, Health and Safety organization governs health, safety and wellness at Abbott’s facilities. Abbott also maintains global policies and standards for managing employee health and safety.",
        "Abbott takes a holistic approach to employee well-being. Abbott’s global wellness programs are designed to meet the unique needs of employees across businesses and geographies and offer a wide range of programs, including supporting the mental, financial and physical health of employees and their families. For example, for over 20 years, Abbott has annually offered Exercise Across Abbott, which is a four-week physical wellness program that encourages employees to team up with colleagues and track how many minutes they exercise each day. Over 22,000 Abbott employees across 72 countries took part in 2020.",
        "During the COVID-19 pandemic, Abbott has taken aggressive steps to limit exposure and enhance the safety of facilities for its employees, including implementing mandatory temperature screening and social distancing, providing and requiring the use of personal protective equipment, and at most U.S. facilities, onsite COVID-19 testing. ",
        "Talent Management",
        "Abbott has an integrated global talent management process that is designed to identify and assess talent across the organization and provide equal and consistent opportunities for employees to develop their skills. All levels of employees participate in Abbott’s annual performance management process to create development plans that support their particular career objectives, and Abbott provides a broad range of training, mentoring and other development opportunities to help its employees meet these objectives. The board of directors conducts an annual Talent Management Review, focusing on development of talent, diversity, and succession planning for critical positions. Similar reviews take place at every level of Abbott to develop talent and diversity across the organization. ",
        "Diversity and Inclusion ",
        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
        "5",
        "Compensation and Benefits",
        "Abbott is committed to building, retaining, and motivating a diverse talent pipeline that can meet the current and future needs of its businesses. To that end, Abbott provides market competitive compensation, healthcare benefits, pension and/or retirement savings plans, and several programs to facilitate employees building an ownership stake in Abbott, including a global long-term incentive program for employees generally beginning at the manager level. Abbott also has procedures and processes focused on providing employees equitable compensation, regardless of race or gender or other personal characteristics."
    ]
}

and I want to get the output below because I am trying to extract the Diversity and Inclusion Section for this particular corporation:

        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
        "5"

Code: Here is the current code I have. I do know that the logic is wrong, but I am unsure on how to proceed on getting in this case the element "Compensation and Benefits" and making the code output anything between the element "Diversity and Inclusion" to the "Compensation and Benefits".

allFilings2021_part1_di_keywords = ["Diversity and Inclusion "]
allFilings2021_part1_di_next_section_keywords = ["Compensation and Benefits"]
di_dic = {}
for k,v in testing_dic.items():
    n = 1
    di_list = []
    for i in range(len(v)):
        if v[i] in allFilings2021_part1_di_keywords:
            if v[i+n] in allFilings2021_part1_di_next_section_keywords:
                di_list.append(v[i:i+n])
                di_dic[k] = di_list
                n += 1
        else:
            continue

Any help is greatly appreciated.



Solution 1:[1]

You could use list.index to get the index of each element. Then you can use slicing to get anything between them:

def find_between(e1, e2, elements):
    return elements[elements.index(e1) + 1:elements.index(e2)]

This would be roughly equivalent to the following code:

def find_between(e1, e2, elements):
    found = []
    iterator = iter(elements)
    for e in iterator:
        if e == e1:
            break
    for e in iterator:
        if e == e2:
            break
        found.append(e)
    return found

The difference is that version 1 raises an exception when an element does not exist while version two just finds nothing if this is the case for e1 and everything after e1 if this is the case for e2.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1