Python: create a list of lists -
i have 2 list
l1= ["apple", "orange"] l2 = ["red", green", "black", "blue"]
i want create list appends both.
l3 = [["apple", "orange"], ["red", green", "black", "blue"]].
so l3[0] =["apple", "orange"]
, l3[1]=["red", green", "black", "blue"]
.
how do above?
just put references in.
l3 = [l1, l2]
note that, if this, modifying l1
or l2
produce same changes in l3
. if don't want happen, use copy:
l3 = [l1[:], l2[:]]
this work shallow lists. if nested, you're better off using deepcopy
:
import copy l3 = [copy.deepcopy(l1), copy.deepcopy(l2)]
Comments
Post a Comment