0

I'm trying to write a function definition that takes a sequence and returns the first and second values. I suspect my code is wrong because it can't take in a list, but I'm not sure. Here's my goal:

Write a function definition named first_and_second that takes in sequence and returns the first and second value of that sequence as a list.

Here's the code I'm having trouble with:

def first_and_second(list):
  return list[0 and 1]

Here's the test of whether I got it right:

assert first_and_second([1, 2, 3, 4]) == [1, 2]
assert first_and_second(["python", "is", "awesome"]) == ["python", "is"]
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Ben Taylor
  • 25
  • 5

3 Answers3

1

There's nothing wrong with how your function "takes in a list", but there's something wrong with how you use the passed list.

return list[0 and 1]

The expression 0 and 1 evaluates to 0:

>>> 0 and 1
0

So that code effectively becomes:

return list[0]

which will only return the 1st element. What you want to do is called slicing, which means getting a subset of a list. From this SO post on Understanding slice notation:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

The correct code is:

def first_and_second(aList):
  return aList[0:2]

which means "get the elements of aList from the index=0 element (the first value) up to the index=1 element (the second value)".

>>> def first_and_second(list):
...   return list[0:2]
>>> print( first_and_second([1, 2, 3, 4]) == [1, 2] )
True
>>> print( first_and_second(["python", "is", "awesome"]) == ["python", "is"] )
True

Also, note that I changed the function parameter list to aList. DO NOT name your parameters/variables as list because that is a built-in type in Python.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • @BenTaylor Great that my answer helped you :) I see that you are a new contributor. Here at Stack Overflow, if an answer helped you, consider marking it as accepted by clicking on the checkmark to the left of the answer. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – Gino Mempin Dec 11 '19 at 05:02
0
def first_and_second(list):
    return [list[0],list[1]]

or

def first_and_second(list):
    return list[0:2]
DarkLeader
  • 589
  • 7
  • 17
0

For a more concise solution, you can use lambda notation:

first_and_second = lambda l : l[:2]

It requires only one keyword instead of two and may therefore be considered a more pythonic way of doing simple things like this.

Since the lambda statement above actually is a function defition, you can just use it as follows:

assert first_and_second([1, 2, 3, 4]) == [1, 2]
assert first_and_second(["python", "is", "awesome"]) == ["python", "is"]
Oblomov
  • 8,953
  • 22
  • 60
  • 106
  • While it works, PEP8 could complain that [you shouldn't assign a lambda expression](https://stackoverflow.com/q/25010167/2745495). – Gino Mempin Dec 10 '19 at 11:23
  • Thanks for the hint. This seems to be one of the more debatable PEP8 rules, however, as one can see here: https://stackoverflow.com/questions/25010167/e731-do-not-assign-a-lambda-expression-use-a-def – Oblomov Dec 10 '19 at 12:01