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.
Exercise: https://www.codecademy.com/courses/learn-python-3/projects/python-gradebook
1 Like
further down in that Lists part of the syllabus you will see the zip
function.
So, you’d do this…
gradebook = zip(subjects, grades)
updated_gradebook = list(gradebook)
print(updated_gradebook)
https://docs.python.org/3/library/functions.html#zip
2 Likes
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)]
print(both)
[['Jerry', '10025'], ['Elaine', '10003'], ['George', '10018'], ['Kramer', '10025']]