Reputation:
Say I had a list:
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
Would there be any way too take out the first 4 values of the list, make them :
[4, 2, 2, 4]
and the put them back in the list so the list now looks like
[4, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
and do all of this without making the list
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
at the start.
Also if possible, can you make a loop to do this for all the 4 values available.
So that the list now looks like:
[4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4]
Upvotes: 2
Views: 86
Reputation: 1
Here is your original list:
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
and you'd like to replace the first 4 items with: [4, 2, 2, 4]
Let's assign the new values a new variable. Let's call it update. So:
update = [4, 2, 2, 4]
Here is your code:
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
update = [4, 2, 2, 4]
for i in range(0, len(update)):
lis[i] = update[i]
print(lis)
Upvotes: 0
Reputation: 383
Given the list of numbers you want to replace the first part of lis
with:
replace_lis = [4, 2, 2, 4]
You can iterate over the length of replace_lis
, and fill in the index spots in lis
accordingly:
for i in range(len(replace_lis)):
if i < len(lis):
lis[i] = replace_lis[i]
The i < len(lis)
is if replace_lis
could be bigger than lis
. If there is no chance of that, you can safely omit that if statement.
Upvotes: 0
Reputation: 7846
Another solution, would be to do the following:
For your first question:
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
ins = [4, 2, 2, 4]
lis[:len(ins)] = ins
print(lis)
Output:
[4, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
For your second question:
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
lis = ins * int(len(lis) / len(ins))
print(lis)
Output:
[4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4]
Upvotes: 0
Reputation: 2251
def list_function(original_list, list_of_value_to_subst):
original_list[:len(list_of_value_to_subst)]=list_of_value_to_subst
print (original_list)
subst_list = list(map(int, list_of_value_to_subst * (len(original_list) // len(list_of_value_to_subst))))
print (subst_list)
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
list_of_value_to_subst=[4, 2, 2, 4]
list_function(lis, list_of_value_to_subst)
output:
[4, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4]
Upvotes: 0
Reputation: 26039
Use itertools.cycle
and itertools.islice
:
from itertools import cycle, islice
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
out = list(map(int, islice(cycle('4224'), len(lis))))
print(out)
# [4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4]
Or a simpler way (without any imports):
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
out = list(map(int, '4224' * (len(lis) // 4)))
print(out)
# [4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4]
Upvotes: 1
Reputation: 311
Just do like this:
lis1 = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
# change first 4 index
lis1[:4] = 4,2,2,4
print(lis1)
# [4, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
lis2 = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
# change every 4 index in loop
for i in range(0,len(lis2),4):
lis2[i:i+4] = 4,2,2,4
print(lis2)
# [4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4]
Upvotes: 1
Reputation: 106445
lis = [0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0]
fill = [4, 2, 2, 4]
for i in range(0, len(lis), len(fill)):
lis[i : i + len(fill)] = fill
print(lis)
This outputs:
[4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 2, 4]
Upvotes: 1