![]() ![]() |
| |||||||||
| Hotspots: Slides and Code TA Corner Comments? Announcements FAQ Static Webspace | ||||||||||
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
def duplicateList(list):
duplicteList = []
i = 0
for item in list[:]:
duplicateList[i] = item
duplicateList[i+1] = item
index = i + 2
print duplicateList
return duplicateListdef duplicateList(list):
duplicateList = []
for entry in list:
duplicateList.append(entry)
duplicateList.append(entry)
print duplicateListtags.
| See the FAQ page. Do you see why you got that error on the first one? When you duplicate the elements, you end up with twice as many elements. You can't make a list longer by referring to indices that don't exist yet. Mark Guzdial |
| list.append(something) puts something at the end of the list. So if the list were ["a","b","c"], doing list.append["d"] would give you ["a","b","c","d"] in the list. Mark Guzdial |
| It's setting "newlist" to an empty list, yes. Mark Guzdial |