Issue
I am trying to play around with a list. I want to replace every list item with data from a file. I would like to get an idea as to how to properly do it.
Currently, I’m using mylist = mylist[:0-1]
and doing the individual replace is quite long. I would like to know of an alternative approach to do it.
mylist = ['exit', 'smoke', 'chef', 'toddler', 'dolphin']
mydata.txt #– to be used to replace the list items
admin
staff
reach
train
Needed Output: #– Printed to screen only
exit smoke chef toddler admin
exit smoke chef toddler staff
exit smoke chef toddler reach
exit smoke chef toddler train
exit smoke chef admin dolphin
exit smoke chef staff dolphin
exit smoke chef reach dolphin
exit smoke chef train dolphin
exit smoke admin toddler dolphin
exit smoke staff toddler dolphin
exit smoke reach toddler dolphin
exit smoke train toddler dolphin
exit admin chef toddler dolphin
exit staff chef toddler dolphin
exit reach chef toddler dolphin
exit train chef toddler dolphin
admin smoke chef toddler dolphin
staff smoke chef toddler dolphin
reach smoke chef toddler dolphin
train smoke chef toddler dolphin
Solution
A pretty simple one (Try it online!):
mylist = ['exit', 'smoke', 'chef', 'toddler', 'dolphin']
mydata = ['admin', 'staff', 'reach', 'train']
for i in reversed(range(len(mylist))):
copy = mylist.copy()
for copy[i] in mydata:
print(*copy)
print()
Answered By – Kelly Bundy
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0