Getting Unique Values from Nested Lists

I am trying to create a list of unique values from a list of nested lists, specifically, this list from the Hurricane project:

# areas affected by each hurricane
areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], ['Jamaica', 'Yucatn Peninsula'], ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'], ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'], ['Bermuda', 'New England', 'Atlantic Canada'], ['Lesser Antilles', 'Central America'], ['Texas', 'Louisiana', 'Midwestern United States'], ['Central America'], ['The Caribbean', 'Mexico', 'Texas'], ['Cuba', 'United States Gulf Coast'], ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], ['Mexico'], ['The Caribbean', 'United States East coast'], ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'], ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'], ['The Caribbean', 'United States East Coast'], ['The Bahamas', 'Florida', 'United States Gulf Coast'], ['Central America', 'Yucatn Peninsula', 'South Florida'], ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'], ['The Caribbean', 'Venezuela', 'United States Gulf Coast'], ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], ['Bahamas', 'United States Gulf Coast'], ['Cuba', 'United States Gulf Coast'], ['Greater Antilles', 'Central America', 'Florida'], ['The Caribbean', 'Central America'], ['Nicaragua', 'Honduras'], ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'], ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'], ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic', 'Turks and Caicos Islands'], ['Central America', 'United States Gulf Coast (especially Florida Panhandle)']]

I tried to iterate through the nested lists with a nested loop:

areas_affected_set = []

for x in areas_affected:
    for y in x:
        if y not in areas_affected_set:
            areas_affected_set.append(x)

But it is not working properly. Am I wrong in thinking I can iterate through the nested lists this way? I tried with len() and a counter variable to do a count of items inside the nested lists and it was unwieldy and also didn’t work.

Any suggestions? I am struggling (which is ok, I want to understand) but even with some searching, I cannot find an answer that works.

Thank you!

Hi,

With nested loops it’s always a good visualization tool to just print the values of one ring of loops at a time to confirm how you’re iterating.

Besides that, note that you can also print conditional statements:
For example

condition = y not in areas_affected_set
print(f"y value: {y}... condition: {condition}")

Taken in a terminal:

>>> y = "s"
>>> condition = y in "sam"
>>> f"y is {y} and it is {condition} that {y} is in 'sam'"
"y is s and it is True that s is in 'sam'"

2 Likes

I think I got it! The idea of watching it iterate through was helpful. This is what I ended up with:

for x in areas_affected:
    for y in x:
        if y not in areas_affected_set:
            condition = y not in areas_affected_set
            print(f"y value: {y}... condition: {condition}")
            areas_affected_set.append(y)
            print(areas_affected_set)

I also noticed a flaw in my own loop after having it print out like that, where I was appending x, instead of y. Thank you for helping me with a new tool in my debugging toolkit, instead of giving me a straight-up answer. That was perfect.

Great!! one small note, note the ordering:

for x in areas_affected:
    for y in x:
        condition = y not in areas_affected_set
        print(f"y value: {y}... condition: {condition}")
        if y not in areas_affected_set:
            
            areas_affected_set.append(y)
            print(areas_affected_set)

This way it will run regardless of the conditional :slight_smile:

Wow, doing that really cleaned up the console output. Thank you again!

1 Like

I feel like this could be an extension to the pep-8 song… “to see what condition my condition was in, yeah yeah, yeah”

2 Likes

Artist: “Guido and the Pep-8”
Album: “Okay Compiler: Or How I Learned to Stop Worrying and Love the Error Messages”

2 Likes