Python 2D Lists Syntax: Brackets vs Parentheses

On the (recently updated) new version of Python Loops in the Computer Science Career Path, the lessons teach that a 2D list should be put in brackets within brackets [["Like", "This"], ["List", "Here"]]. However, on the Python Gradebook project, the last semester gradebook is written (and prints) as follows:

last_semester_gradebook = [("politics", 80), ("latin", 96), ("dance", 97), ("architecture", 65)]

In the same project, the user creates a 2D list, with the hint reiterating what was said in the lesson: that it should all be bracketed.

Is last_semester_gradebook not really a 2D list? Are parentheses a valid way of formatting 2D lists? Why are we able to add lists with parentheses and brackets together and still have them work? Is this a preference issue, or a Python 3 issue, or something else entirely?

I Googled for a while, but couldn’t find anything that wasn’t about NumPy, which I don’t yet know anything about, and the answers weren’t particularly enlightening.

The brackets, or square brackets [a, b] to be more precise indicate a list, either creating one with a list literal or how they’re represented in string format. The parentheses (a, b) are the representation of a tuple rather than a list.

This project used to use zip to combine two lists and the members of the zip object created are a tuple. So the newer instructions create a list of lists whereas the old instructions created a list containing a series of tuples.

So they’re not the same. I think there’s now an article in the syllabus introducing tuples at some point. The following link might help but if not have a look for tuples rather than nested lists-

2 Likes

Ah, ha! I thought I’d previously used zip!
And when I went back to check on this yesterday all of my previous code was gone, yet the lesson was marked as complete. So, I re-did it w/o using zip.

1 Like

Yup, let me clarify: Last week the project used tuples and zip, and afaik, asked us to write our answers as tuples, as well. As of this week, the material on 2D lists was newly introduced in the lessons (as well as remove), which means the actual project has changed a lot.

However, the last_semester_gradebook, which is provided for you, is (as you say) in tuple format, but the new gradebook is supposed to be written as a 2D list (using brackets), and yet the instructions have us add them together, which results in a print [("That", "looks"), ["like", "this"]], so I’m just wondering how that could be, if they’re not the same…

Yeah, they changed a bunch of stuff this week, according to the changelog. According to (I think) the Web Dev/JavaScript changelog, changes made in lessons don’t change the completion level (if you did it before, it’ll stay marked as complete), but they do advise going back and doing the lessons again when you have time. :sweat_smile:

I also found out about the change because I saw the new code didn’t use zip. Haha.

1 Like

Oh, yea, I saw the mention of them updating some of the python stuff, but, I only looked at it b/c someone else brought it up. hah. I should go see what else I need to re-do now! :slight_smile:

1 Like

What you describe is a list with one element that’s a tuple (the tuple itself contains references to two further objects, the two strings) and one element that’s a list (also containing strings). The elements within a list as you’ve probably already seen don’t all have to be the same type.

Looking at the current page it seems like an oversight to provide you with a list containing tuples, and tasks which then ask you to create two new lists (which are never used) and then to create a third new list which only contains other lists.

None of this stops you completing any tasks and if they’re completed in a linear fashion there shouldn’t be any bugs, it just seems a bit redundant. Sticking with a list of lists would be more straightforward to avoid introducing new data types which haven’t yet been covered so I’ll raise it as a suggestion.

1 Like

My guess is that they just didn’t change the starter code from a tuple to a 2D list when they updated the rest of the unit. 2D lists weren’t part of the unit/project until this week.

Reporting it would be good! Thanks!