Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: list manipulation

Tags:

python

list

I have a list L of objects (for what it's worth this is in scons). I would like to create two lists L1 and L2 where L1 is L with an item I1 appended, and L2 is L with an item I2 appended.

I would use append but that modifies the original list.

How can I do this in Python? (sorry for the beginner question, I don't use the language much, just for scons)

like image 374
Jason S Avatar asked Dec 13 '22 22:12

Jason S


2 Answers

L1 = L + [i1]
L2 = L + [i2]

That is probably the simplest way. Another option is to copy the list and then append:

L1 = L[:]       #make a copy of L
L1.append(i1)
like image 154
interjay Avatar answered Jan 02 '23 13:01

interjay


L1=list(L)

duplicates the list. I guess you can figure out the rest :)

like image 34
o0'. Avatar answered Jan 02 '23 11:01

o0'.