I am trying to combine this code:
subjects = ["physics", "calculus", "poetry", "history"]
grades = [98, 97, 85, 88]
output needs to be
Combining two lists doesn't have "output". It has a result. You get output when you either print
the result, or in interactive use because the REPL happens to print it for you. It's important to say clearly what you mean.
[physics, 98],[calculus, 97],...
When I use zip to combine I get: <zip object at 0x7f71c6574c88>
Yes, that's the result of combining the lists in the way you wanted. It's an iterator, which means you can iterate over it and it will yield first the tuple ('physics', 98)
, and then the tuple ('calculus', 97)
and so on.
Here is the documentation for zip: link. It's worth reading the docs so you know what built-in functions exist.
This is why the existing answers actually said list(zip(...))
. It iterates over each value to populate the list.
However, what you actually care about is the output, in a form which doesn't match the built-in formatting of a list of tuples. You want a string containing a comma-separated sequence of two-element lists.
Note that list
is printed with []
, and tuple
is printed with ()
, by default. This is also described in the documentation, and it's also worth knowing your language's built-in types.
','.join(str(list(t)) for t in zip(subjects, grades))