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 Introduction to Data Visualization with Matplotlib Chart Toppers Scatter Plot

Anshul Laikar
Anshul Laikar
4,428 Points

What does groupby do exactly?

I read the itertools thing on python's website. It says that groupby basically divides the list whenever the iterator changes values. But what then? Where does it store these groups? How exactly does it iterate? I did not really understand that.

Anshul Laikar
Anshul Laikar
4,428 Points

I was messing around with the iterator and came by this:

from itertools import groupby
import csv
import matplotlib.pyplot as plt
input_file = 'iris.csv'

with open(input_file, 'r') as iris_data:
    irises = list(csv.reader(iris_data))

colors = {"Iris-setosa": "#2B5B84", "Iris-versicolor": "g", "Iris-virginica": "purple"}
irises.pop()
i=0
for group in groupby(irises, lambda i: i[4]):
    cat_ir= list(group)
    print(cat_ir)
    print(species)
    print(i)

    i+=1   

I haven't mentioned species anywhere in the given program or named it as a variable. How does print(species) still work?

1 Answer

okay, that's interesting. Have you found its answer yet?