1

I want to move part of the list to another list with a loop But I have problem

Something like that (But this code is bug)

mylist = [1,2,3,4,5,6,7,8]

i=[]

for i in mylist.append(range(1,5)) :
    print(i)

I want the numbers 2 to 6 to be in i


Thanks to everyone I solved this problem With this code

mylist = [1, 2, 3, 4, 5, 6, 7, 8]

i = []
b=0
c=1
while b < 5 :
    i.append(mylist[c])
    print(i)
    b+=1
    c+=1
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 1
    `append` doesn't return anything. – dspencer Apr 01 '20 at 03:35
  • Please take a look at the duplicate post I've linked above - in particular the answer with the highest votes. The typical way to do this is with `list.extend`, but if you want to append elements in a list one by one, you will need to modify your loop. Have a look at the middle of the answer to show you how. – rayryeng Apr 01 '20 at 03:43
  • I used 'extend' but still have "'NoneType' object is not iterable" error and have no idea about it I searched a bit but I didn't understand anything – amir armaniya Apr 01 '20 at 03:59

0 Answers0