Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Spring 2004: Duplicate a List

Comments, answers, and questions go here:

(Link back to Sp2004 Midterm 2 Review)


Here's what I got:
def duplicateList(list):
  for i in list:
    print i,i


Why is this not working?? this seems to me exactly what was done in breakout.
Amelia Cipolla


def duplicateList(string):
mylist=[]
for i in string:
mylist=mylist+[i]+[i]
return mylist



Amelia, it works perfectly!
>>> duplicateList([1,2,3])
[1, 1, 2, 2, 3, 3]
Mark Guzdial


I do not really understood "i". Does it just alwaus represent a number?

No – an "item" in the list. (It doesn't actually take a string as input.) Mark Guzdial

what's the difference between print dupList[(1,2,3,4)] & just asking jes to dupList[(1,2,3,4)]?? because i seem to be getting the same answer for both

if 'i' doesn't take a string as an input, what DOES it take as input?

Things delimeted by [] are lists. Things delimeted with () are inputs, or "tuples" (which we don't discuss in this class). This program will work for tuples or lists, but that's not always true – lists can do things that tuples can't. Mark Guzdial


I tried putting both def above in JES and running them, and did not get anything. Am I doing something wrong, or are the def wrong?

you have to put the "[ ]" brackets when entering it in JES

Do you have to use the variable i, or can you use another variable, like x? Does JES recognize i, or do we choose an arbitrary variable?



Link to this Page