Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Sets Set Math

Anthony Grodowski
Anthony Grodowski
4,902 Points

Is there a way to return multiple items from for loop?

///SOLVED BY MYSELF\\ From the function I get only 1 item, while in my workspace code ( https://w.trhou.se/ui90o6esq7 ), which ends with print I get everything I need - full set of items. Is there a possibility to return multiple values from for loop? As far as I'm concerned for loop returns only the first item. It's important to me because later I need to join together these items to one list and then return it.

sets.py
COURSES = {
    "Python Basics": {"Python", "functions", "variables",
                      "booleans", "integers", "floats",
                      "arrays", "strings", "exceptions",
                      "conditions", "input", "loops"},
    "Java Basics": {"Java", "strings", "variables",
                    "input", "exceptions", "integers",
                    "booleans", "loops"},
    "PHP Basics": {"PHP", "variables", "conditions",
                   "integers", "floats", "strings",
                   "booleans", "HTML"},
    "Ruby Basics": {"Ruby", "strings", "floats",
                    "integers", "conditions",
                    "functions", "input"}
}

def covers(objectt):
    list_keys = [k for k in COURSES]
    list_values = [v for v in COURSES.values()]
    for topics in list_values:
        if objectt & topics:
            mutual_courses = objectt & topics
            return[(list(COURSES.keys())[list(COURSES.values()).index(topics)])]

2 Answers

Anthony Grodowski
Anthony Grodowski
4,902 Points

Alright, I've got this

def covers(objectt):
    my_list = []
    list_keys = [k for k in COURSES]
    list_values = [v for v in COURSES.values()]
    for topics in list_values:
        if objectt & topics:
            mutual_courses = objectt & topics
            my_list.append((list(COURSES.keys())[list(COURSES.values()).index(topics)]))
    return my_list       

Nice work! Glad you solved it. A trick that may come in handy next time; you can loop through keys and values in a dictionary with ease using .items(). Example:

for key, value in my_dict.items():
    # some logic