I have two two lists (mylist and myline). I want to add each element of mylist to the first element of myline, such that for each element in mylist I get a new myline where myline[0] = mylist[x] + "_" + myline[0]. For example:
mylist = ["a","b","c"]
myline = ["blah", "jah", "hah"]
I want to get in return:
["a_blah", "jah", "hah"]
["b_blah", "jah", "hah"]
["c_blah", "jah", "hah"]
But when I run the following code, it doesn't work:
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ["a","b","c"]
>>> myline = ["blah", "jah", "hah"]
>>> for x in mylist:
... print x
...
a
b
c
>>> for x in mylist:
... secondline = myline
... secondline[0] = x + "_" + myline[0]
... print x
... print myline
... print secondline
...
a
['a_blah', 'jah', 'hah']
['a_blah', 'jah', 'hah']
b
['b_a_blah', 'jah', 'hah']
['b_a_blah', 'jah', 'hah']
c
['c_b_a_blah', 'jah', 'hah']
['c_b_a_blah', 'jah', 'hah']
>>>
It seems that myline gets changed as well, even though I never specifically change myline (I always create a new "secondline" based on "myline"). I've been trying to figure this out and I feel like I'm going to lose my mind! Where am I going wrong? Thanks!