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

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -