I would like to re-order the values of the lists inside a_list
.
This is my current snippet:
a_list = [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]]
order = [1, 0, 2]
a_list = [a_list[i] for i in order]
print(a_list)
This is my current output:
[['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
This is my desired output:
[['b', 'a', 'c'], ['b', 'a', 'c'], ['b', 'a', 'c']]
You need to access each sublist of a_list
, and then reorder within that sublist. Using list comprehension, it would be like:
a_list = [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]]
order = [1, 0, 2]
a_list = [[sublst[i] for i in order] for sublst in a_list]
print(a_list) # [['b', 'a', 'c'], ['b', 'a', 'c'], ['b', 'a', 'c']]
Your current code reorders the sublists themselves; i.e., for example, if you started with
a_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
then the result would have been
[['d', 'e', 'f'], ['a', 'b', 'c'], ['g', 'h', 'i']]