Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 16th, 2013, 12:58 PM
 jpg123
Registered User

Join Date: Jun 2013
Posts: 20
Time spent in forums: 19 h 17 m 30 sec
Reputation Power: 0
Ebook question help needed

im having trouble with this exercise.

Exercise 10.2 Write a function called chop that takes a list and modifies it, removing the first and
last elements, and returns None.

Then write a function called middle that takes a list and returns a new list that contains all but the
first and last elements.

I think I can do the second part, I came up with-

Code:
```t=[1,2,3,4]

def middle(t):
return t[1:len(t)-1]

print(middle(t))```

The first part has me stumped tho, I came up with this-

Code:
```t=[1,2,3,4]

def chop(t):
del t[0:len(t)-1]

print(chop(t))```

It returns None but it doesn't look right and im not sure that this is what the question is asking for. Some advice would be appreciated.

#2
August 16th, 2013, 02:29 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,215
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 22 m 49 sec
Reputation Power: 455
Useful trick: Index from the right end

[code>>> t=[1,2,3,4]
>>> def chop(t):
del t[0], t[-1]
# or
# del t[0]
# del t[-1]

>>> chop(t)
>>> t
[2, 3]
>>> def middle(t):
return t[1:-1]

>>> middle(t)
[]
>>> [/code]
Nykinum agrees!
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Ebook question help needed