← Back to challenges

Destructuring Lists II

PythonMediumlanguage_fundamentalsarrays

Instructions

We can destructure lists like this:

arr = ["1", "2", "3"]
a, b, c = arr

but what if the list has nested lists, like the following?

arr = ["cars", "planes", ["trains", ["motorcycles"]]]

trans1 = arr[0]
trans2 = arr[1]
trans3 = arr[2][0]
trans4 = arr[2][1][0]

println(trans1) # outputs "cars"
println(trans2) # outputs "planes"
println(trans3) # outputs "trains"
println(trans4) # outputs "motorcycles"

Can you use list destructuring to assign all four variables at once?

Notes

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Find the Total Number of Digits the Given Number Has