#Confused about Lists and Tuples

16 messages · Page 1 of 1 (latest)

slim jungle
#

I read that from https://docs.python.org/3/library/typing.html:

Type checker error: list only accepts a single type argument:

y: list[int, str] = [1, 'foo'] ...

list only accepts one type argument, so a type checker would emit an error on the y assignment above.

But, the perfect_score function in the Making the Grade exercise returned just such a list. Am I missing something?

olive anvil
#

Hi Pebble5900 👋

Yes, you are missing something. This SO post: https://stackoverflow.com/a/74794196 may or may not help your confusion. 🙂

Type hits are optional for the Python language and are ignored/not enforced by the default (CPython) runtime.
In CPython, lists (and tuples) can contain different data types at the same time -- those data types do not need to match (even if they often do). The restriction you show is for the typing module only, and the errors only apply if you are using a type checker.

Since we are not using a typchecker in our Python track tooling (nor do we have plans to use one in the near future), its not an issue.

thin siren
#

Note, list[str, int] is incorrect as list[...] accepts a single type. If you want "str or int" you should be using an "or" type operator, ie list[typing.Union[str, int]] or list[str | int] (the | was a newer addition)

#

But as Bethany said, you don't need to worry about types if you don't want to.

olive anvil
#

But as Bethany said, you don't need to worry about types if you don't want to.

I'd modify that to say that you don't need to worry about typehints and type aliases right now.. BUT -- you do need to worry about the differences between lists (mutable sequences) and tuples (immutable sequences) or str vs int, etc. 😉

thin siren
#

Yup. My bad. That should have read "type hints" or "type annotations".
Given that the title says "Lists and Tuples" and you didn't mention tuples yet,

  • lists are usually used to hold "zero or more of a repeating type of data" where the exact code doesn't matter much, eg a list of students. The exact number of students shouldn't matter a whole lot. The order often doesn't matter much, either.
  • tuples are usually used to hold an exact number of entries, eg (x, y) coordinates or (r, g, b) colors where you expect exactly n values and the position of the value means something. Usually swapping orders in a tuple will break something, as will having an unexpected number of elements.

Given that lists are "zero or more of thing X", you get list[X]. Tuples are "exactly X, Y, Z" so the type annotation is tuple[X, Y, Z] which might be tuple[int, int] or tuple[int, str, str] etc.

native stagBOT
#

If everything is resolved, we ask that the person who posted the request react to the top/original post with a :white_check_mark: (:white_check_mark:). This indicates to others that this issue has been resolved and locks the thread.
If all the tests pass and you want to further improve your solution, we encourage you to use the "Request a Code Review" feature on the website!

thin siren
#

@slim jungle

slim jungle
#

Create the function perfect_score(student_info) with parameter student_info. student_info is a list of lists containing the name and score of each student: [["Charles", 90], ["Tony", 80]]. The function should return the first [<name>, <score>] pair of the student who scored 100 on the exam.

If no 100 scores are found in student_info, an empty list [] should be returned.

So, is the exercise just expressing it incorrectly?

thin siren
#

The exercise does not use tuples. If I were storing name-score pairs, I would use tuples. This exercise stores it in a list of lists. That may not be perfectly aligned with how things are often done, but it does work.

#

This exercise is part of the syllabus. It is there to show how loops work. It's very likely loops are shown prior to tuples and the exercise wants to stick to concepts shown prior in the syllabus. Writing a syllabus and exercises to go with it that only use previously shown concepts is ... hard.

thin siren
#

@slim jungle Did you solve it?

slim jungle
#

I solved the exercise. I was confused because the input was a list of a list with a string and an integer, ie, list[list[str, int]]. The next exercise is on tuples, which is where I learned that a list[str, int] should result in an error.

thin siren
#

You can totally put multiple data types in one list! The annotation is just list[str | int] and not list[str, int]

#

There's nothing wrong with having a list with many data types.

native stagBOT
#

If everything is resolved, we ask that the person who posted the request react to the top/original post with a :white_check_mark: (:white_check_mark:). This indicates to others that this issue has been resolved and locks the thread.
If all the tests pass and you want to further improve your solution, we encourage you to use the "Request a Code Review" feature on the website!