5
child = []
parent = [1, 3, 5, 7, 9]
index = 2
child.append(parent[:index])

When I have this code run, instead of returning me a child list of

child = [1, 3]

I get a child list of:

child = [[1, 3]]

Is there a single line method of copying a few objects of a list into another list without making it into a nested list?

martineau
  • 119,623
  • 25
  • 170
  • 301
benj rei
  • 329
  • 2
  • 5
  • 12

1 Answers1

2

Try this:

child.extend(parent[:index])
user20160
  • 1,396
  • 7
  • 10