Hi
I try to copy a list to another list but it dosent work

def kopi(x,y):
    i=0
    for i in range (1,len(x)-1):
        
        x[i]= y[i-1]
        i+=1
    print y

I dont know what is the problem with my code.
thanks alot for your help.

1) You don't return x from the function so it is not visible outside the function.
2) You have multiple variables named "i" (Please don't use, i, l, or o as they look like numbers and there as 23 other letters that work just as well) for i in range (1,len(x)-1): is one, and i=0, i+=1 is the second.
3) The proper way to add to a list is to use append. Following your original idea

def kopi(x,y):
    for j in range (0, len(x)):
        x.append(y[j])        
    print y

You can also use x=y[:]
If you want x[0] + y then you can do
new_x= []
new_x,append(x[0])
new_x += y
If you want x to be y[1]-->y[len(y)], omitting the first element, you can use z=y[1:].
If this doesn't help, then you will have to be more explicit about "doesn't work". That doesn't say much.

Hi
Thank you very much for your replay.
yes, I use i, very often but it is the same i, everywhere. When i=0 I mean the first element in my list then i++ the second ..etc. x(i) I mean the content of the element.
I don’t want to make a exact copy of the x, I want to copy all element from x-1 to y.
for example if I have( 9,6,2,4 )in may x then I want to move this element minus one to y then in y I will have (8,5,1,3).
x and y are two parameters in my function when I call my function I send my original lists to function instead of x and y.
when I print y I get only (None,None,…)
Thanks again

1) You don't return x from the function so it is not visible outside the function.
2) You have multiple variables named "i" (Please don't use, i, l, or o as they look like numbers and there as 23 other letters that work just as well) for i in range (1,len(x)-1): is one, and i=0, i+=1 is the second.
3) The proper way to add to a list is to use append. Following your original idea

def kopi(x,y):
    for j in range (0, len(x)):
        x.append(y[j])        
    print y

You can also use x=y[:]
If you want x[0] + y then you can do
new_x= []
new_x,append(x[0])
new_x += y
If you want x to be y[1]-->y[len(y)], omitting the first element, you can use z=y[1:].
If this doesn't help, then you will have to be more explicit about "doesn't work". That doesn't say much.

First, the following code will explain what I mean by 2 variables named "i". The second x prints out a different value and a different element for x_list. If this code changed for loop value for x to 1, then the loop would never end in this example because x would never get to 5. It, in fact, repeatedly gives a another block of memory named "x" the value of 1.

x=0
x_list=[ 1, 2, 3, 4, 5 ]
for x in range(0, 5):
   print "\nfor loop x =", x, x_list[x]
   x = 1
   print "     2nd x =", x, x_list[x]
   
##-----  output
for loop x = 0 1
     2nd x = 1 2
     
for loop x = 1 2
     2nd x = 1 2
          
for loop x = 2 3
     2nd x = 1 2
               
for loop x = 3 4
     2nd x = 1 2
                    
for loop x = 4 5
     2nd x = 1 2

If you want to subtract one from each element in x, then it would be something like this, assuming that each element is appended/added to y instead of replacing an existing element

def kopi(x,y):
    for each_el in x:
        print "before subtracting", each_el,
        y.append(each_el-1)
        print "  after subtracting", each_el-1
    print y
    return y

If every element in x is not an integer or float, you will get an error when you try to subtract.

You have to be careful not to create a list alias. Something you do inadvertantly with a nested list:

# make a true copy of a nested list

import copy

nested_list = [1, [2, 3], 4]
copied_list = copy.deepcopy(nested_list)

# a regular list copy looks alright at first
copied_list2 = nested_list[:]

print nested_list   # [1, [2, 3], 4]
print copied_list   # [1, [2, 3], 4]
print copied_list2  # [1, [2, 3], 4]

print '-'*20

# change the orignal list
nested_list[1][0] = 99

# but the test shows a surprise!
print nested_list   # [1, [99, 3], 4]
print copied_list   # [1, [2, 3], 4] 
print copied_list2  # [1, [99, 3], 4]  oops!

Hi
Thank you very much it was exact what I needed to know, in this line:

y.append(each_el-1)

Did we create a new list y? Which is very important for my program.
Can we use extend instead of append?

The function extend() adds the contents of another list to your list ...

listA = [1, 2, 3]
listB = [4, 5, 6]
# make a simple non-alias copy
listC = listA[:]

listA.append(listB)
print listA   # [1, 2, 3, [4, 5, 6]]

listC.extend(listB)
print listC   # [1, 2, 3, 4, 5, 6]
This question has already been answered. Start a new discussion instead.