These are two lists:
subjects = ["physics", "calculus", "poetry", "history"]
grades = [98, 97, 85, 88]
How would I combine them to create a two-dimensional list which looked like this without doing it manually:
gradebook = [["physics", 98], ["calculus", 97], ["poetry", 85], ["history", 88]]
In the actual exercise, I’m required to do it manually. But I was just wondering whether there was a way to actually combine both lists through some other means.
further down in that Lists part of the syllabus you will see the
So, you’d do this…
gradebook = zip(subjects, grades)
updated_gradebook = list(gradebook)
I guess this is now a tuple. What about making it a two-dimensional list?
There are a few ways I guess. What have you found?
Have you gone over list comprehensions yet?
If so, you could do something like this:
names = ['Jerry', 'Elaine', 'George', 'Kramer']
codes = ['10025', '10003', '10018', '10025']
both = [list(a) for a in zip(names, codes)]
[['Jerry', '10025'], ['Elaine', '10003'], ['George', '10018'], ['Kramer', '10025']]