2
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
  • 112,593
  • 23
  • 157
  • 280
benj rei
  • 299
  • 2
  • 5
  • 12

1 Answers1

2

Try this:

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