We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
marksheet is a 2D array, something like [["Sally", 20.3], ["John", 19.5], ["Susan", 21.2]].
So name, marks effectively destructures each element in the 2D array. For every element, name gets bound to the first item, e.g. "Sally", and marks gets bound to the second item, e.g. 20.3. So we iterate through the 2D array, and for each element, we return only the second item in the subarray, and we end up with a 1D array like this: [20.3, 19.5, 21.2].
The code is slightly misleading because as the list comprehension iterates over the 2D array marksheet, it's only looking at 1 mark at a time. So perhaps, it should have been written [mark for name, mark in marksheet].
Does that make sense?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Nested Lists
You are viewing a single comment's thread. Return to all comments →
marksheet is a 2D array, something like
[["Sally", 20.3], ["John", 19.5], ["Susan", 21.2]]
.So
name, marks
effectively destructures each element in the 2D array. For every element,name
gets bound to the first item, e.g."Sally"
, andmarks
gets bound to the second item, e.g.20.3
. So we iterate through the 2D array, and for each element, we return only the second item in the subarray, and we end up with a 1D array like this:[20.3, 19.5, 21.2]
.The code is slightly misleading because as the list comprehension iterates over the 2D array
marksheet
, it's only looking at 1mark
at a time. So perhaps, it should have been written[mark for name, mark in marksheet]
.Does that make sense?