Bobby James
Bobby James

Reputation: 47

Python beginner (lists)

I need to write a function named make_ends that receives one parameter - a list. It returns a new list that contain the first and last items of the input list.

Here is my code, but it doesn't separate it into a list. I'm a little confused about how to go about this. Do I need to use the .join feature? If so, how do I do that? I think I am close.

def make_ends(x):
    return x[0], x[-1]

Here was my earlier build, but it didn't do anything except return the original string:

def go_right(str):
    if str >= 2:
            a = str[-2:0] + str
            return a

What was wrong with that?

Thanks for the help everyone.

Upvotes: 0

Views: 168

Answers (6)

Johnny
Johnny

Reputation: 205

There are indeed multiple ways of tackling this task

One would be the most common and probably most conventional

def make_ends(x):
    return [x[0], x[-1]]

Another method

def make_ends(x):
    finished = []
    finished.append(x[0])
    finished.append(x[1])
    return finished

Also, you could turn the string into a list, allowing the function to work with strings

def make_ends(x):
    x = list(x)
    return [x[0], x[-1]]

Upvotes: 1

abarnert
abarnert

Reputation: 365657

You're actually very close; the only problem is that you're returning a tuple instead of a list. (Whatever a and b are, a, b is a tuple of those two things, just like [a, b] is a list of those two things.)

One way to solve this is to call list to make a list out of the tuple:

def make_ends(x):
    return list((x[0], x[-1]))

But the easy way to do it is just to create a list in the first place:

def make_ends(x):
    return [x[0], x[-1]]

You then ask another question:

Here was my earlier build, but it didn't do anything except return the original string:

def go_right(str):
    if str >= 2:
            a = str[-2:0] + str
            return a

Let's go through this step by step.

First, str >= 2 is comparing a string to a number. In Python 2.x, either all strings are bigger than all numbers, or all strings are smaller than all numbers. (That's left up to the implementation.) So, this isn't a very useful check.

Maybe you wanted to check if len(str) >= 2 instead? But even then, I'm not sure what that would get you. If the length were 0 or 1, what would you want to do? As it is, you'd return None, which probably isn't right.

Meanwhile, str[-2:0] asks for all elements that come after 2-before-the-end, but before the start. There are no elements before the start, so this is empty. Then you add the original value to this empty collection, so you get the original value.


Also, as a side note, calling a parameter str is a bad idea. Besides the fact that it hides the built-in str (which is a function that turns anything into its string representation), it also strongly implies that what you're dealing with is a string, not a list.

Upvotes: 4

Benjamin Hodgson
Benjamin Hodgson

Reputation: 44634

You're currently returning a tuple, not a list. Simply wrap it in square brackets to make it a list:

def make_ends(x):
    return [x[0], x[-1]]

However, I'd question why you want a list in the first place. You know you're only returning two items, so you don't need it to be mutable - it seems to me perhaps that a tuple is what you want after all here.

Upvotes: 1

Jainathan Leung
Jainathan Leung

Reputation: 1157

def make_ends(x):
  return [x[0], x[-1]]

Upvotes: 0

GoingTharn
GoingTharn

Reputation: 1131

you're very close. You just have to cast the return into a list.

return [x[0], x[-1]]

Upvotes: 0

JaredPar
JaredPar

Reputation: 754585

Try the following

def make_ends(x):
  return [x[0], x[-1]]

In the current form instead of a list you are creating a tuple. This is what the comma operator does when put between values

Upvotes: 1

Related Questions