Inserting elements of one list into another list in Python
Question:
I have two lists J1
and Leftover
. I want to insert elements of Leftover
into J1
at locations of 0
elements of J1
and ensure that new J1
has all non-zero elements in ascending order. I want the length of old and new J1[0]
to be the same. I present the current and expected outputs.
import numpy as np
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
for i in range(0,len(Leftover)):
J1.insert(Leftover[i],i)
print("J1 =",J1)
The current output is
J1 = [[0, 2, 0, 0, 0, 9, 0], 0, 1]
The expected output is
J1 = [[1, 2, 4, 0, 0, 9, 0]]
Answers:
This code sorts even elements that are 0
.
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
J1[0].extend(Leftover)
J1[0].sort()
# J1:
# [[0, 0, 0, 0, 0, 1, 2, 4, 9]]
Since J1
is a list with a single list inside, you need to add new elements to J1[0]
instead of J1
.
Note that the solution doesn’t require numpy
.
To replace elements that are 0
(instead of lengthening the list), do this:
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
for lftovr in Leftover:
for i in range(len(J1[0])):
if J1[0][i] == 0:
J1[0][i] = lftovr
break
# J1:
# [[1, 2, 4, 0, 0, 9, 0]]
J1[0].sort()
# J1:
# [[0, 0, 0, 1, 2, 4, 9]]
Note that sorting elements without rearranging the 0
s is an underspecified task from your post’s description: What if the original list is something like [3, 1, 0, 4, 0, 0, 9]
and you would like to insert 2
and 5
? To get something like [1, 2, 3, 4, 5, 0, 9]
, we would need to rearrange the original order of the items; pure replacement of 0
s can’t do the job. But if we sort, we might as well sort the whole list (including the 0
s).
Some assumptions you might be making that would make the problem better-defined:
- the original list’s non-
0
elements are already in sorted order
- the required insertions can be done without rearranging any of the initial non-
0
elements
- there is enough space in the list
If you want to insert Leftover in first element of J1 and sort first element of J1 in ascending order. Then the code will be:
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
J1[0].extend(Leftover)
J1[0].sort()
print("J1 =", J1)
Output will be: J1 = [[0, 0, 0, 0, 0, 1, 2, 4, 9]]
I have two lists J1
and Leftover
. I want to insert elements of Leftover
into J1
at locations of 0
elements of J1
and ensure that new J1
has all non-zero elements in ascending order. I want the length of old and new J1[0]
to be the same. I present the current and expected outputs.
import numpy as np
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
for i in range(0,len(Leftover)):
J1.insert(Leftover[i],i)
print("J1 =",J1)
The current output is
J1 = [[0, 2, 0, 0, 0, 9, 0], 0, 1]
The expected output is
J1 = [[1, 2, 4, 0, 0, 9, 0]]
This code sorts even elements that are 0
.
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
J1[0].extend(Leftover)
J1[0].sort()
# J1:
# [[0, 0, 0, 0, 0, 1, 2, 4, 9]]
Since J1
is a list with a single list inside, you need to add new elements to J1[0]
instead of J1
.
Note that the solution doesn’t require numpy
.
To replace elements that are 0
(instead of lengthening the list), do this:
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
for lftovr in Leftover:
for i in range(len(J1[0])):
if J1[0][i] == 0:
J1[0][i] = lftovr
break
# J1:
# [[1, 2, 4, 0, 0, 9, 0]]
J1[0].sort()
# J1:
# [[0, 0, 0, 1, 2, 4, 9]]
Note that sorting elements without rearranging the 0
s is an underspecified task from your post’s description: What if the original list is something like [3, 1, 0, 4, 0, 0, 9]
and you would like to insert 2
and 5
? To get something like [1, 2, 3, 4, 5, 0, 9]
, we would need to rearrange the original order of the items; pure replacement of 0
s can’t do the job. But if we sort, we might as well sort the whole list (including the 0
s).
Some assumptions you might be making that would make the problem better-defined:
- the original list’s non-
0
elements are already in sorted order - the required insertions can be done without rearranging any of the initial non-
0
elements - there is enough space in the list
If you want to insert Leftover in first element of J1 and sort first element of J1 in ascending order. Then the code will be:
J1 = [[0, 2, 0, 0, 0, 9, 0]]
Leftover=[1,4]
J1[0].extend(Leftover)
J1[0].sort()
print("J1 =", J1)
Output will be: J1 = [[0, 0, 0, 0, 0, 1, 2, 4, 9]]