0

My requirement is l1 should be fixed length & l2 length will be vary dynamically.

l1 = [None] * 24
l2 = [1,2,3,4,5,6,7,8,9,10] 
(or)
l2 = [1,2,3,4,5] 
(or)
l2 = list(range(40))

Now I need to append or insert the second list or array from the starting position. My final output expected as below ( Means after append or insert the L1 length always 24)

Case 1:

    l1 = [1,2,3,4,5,6,7,8,9,10, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

Case 2:

    l1 = [1,2,3,4,5,None, None,None, None,None None, None, None, None, None, None, None, None, None, None, None, None, None, None]

Case 3:
(Ignore after 24th index)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

Is there any optimal solution using python list or numpy array or any other lib?

thangaraj1980
  • 141
  • 2
  • 11

1 Answers1

0

You can use subscripting to assign l2 (or a part of it) to a subset of l1 (i.e. using the smallest length):

l1[:len(l2)] = l2[:min(len(l1),len(l2))]
Alain T.
  • 40,517
  • 4
  • 31
  • 51