I've started learning python just a few days back and am stuck with a problem.
If, list=['tuesday','wednesday','friday','sunday']
I'm trying to write a function so i could pass in a specific index number and a value to insert and rather than using list.insert(0,'monday')
then list.insert(2,'thursday')
and so on I could directly pass the indexes eg. ind = [0,2....]
and values = ['monday','thursday'....]
and get a result as
list = ['monday', 'tuesday','wednesday', 'thursday', 'friday', 'saturday', 'sunday']
tried
list = ['tuesday','wednesday','friday','sunday']
indexes = [0,1,2,3]
values = ['monday','thursday','saturday']
for index, value in (indexes, values):
result = list.insert(index, value)
print (result)
and many other things but failed...help would be appreciated
CodePudding user response:
There you have the solution:
list = ['tuesday','wednesday','friday','sunday']
indexes = [0,3,5]
values = ['monday','thursday','saturday']
result = list
for index in range(len(indexes)):
result.insert(indexes[index], values[index])
print (result)
Please take into account return is just for functions
CodePudding user response:
Try this
days = ['tuesday','wednesday','friday','sunday']
indexes = [0,3,5]
values = ['monday','thursday','saturday']
for i in range(len(indexes)):
days.insert(indexes[i], values[i])
return days