I am aware that we can unpack items inside a list. For example this works:
mynestedlist = [[1,2],[3,4],[5,6],[7,8]]
for a,b in mynestedlist:
print(a)
print(b)
Results:
1
2
3
4
5
6
7
8
Is it possible to unpack a list that looks like this? I am looking for a solution where I can control the level of flattening, this question is not simply to arbitrary flatten an irregular list.
myotherlist = [[1,2,['A','B']],[3,4],[5,6],[7,8]]
When I try with the same approach, I receive:
ValueError: too many values to unpack (expected 2)
So I tried with the following approach:
for a,b,c in mynestedlist:
print(a)
print(b)
print(c)
In this case I receive:
ValueError: not enough values to unpack (expected 3, got 2)
How do I unpack nested lists that may or may not vary in length without having to completely flatten all contained items?
Desired Results:
1
2
[A,B]
3
4
5
6
7
8