FAQ: Learn Python - Lists and Functions - Passing a range into a function

This community-built FAQ covers the “Passing a range into a function” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Passing a range into a function:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

I am really confused on the keyword argument section fo python functions can someone help me

I was doing a basic mistake but I rectified it soon after I clicked the solution button. It gave me the following code:

def my_function(x):
  for i in range(0, len(x)):
    x[i] = x[i]
  return x

print my_function(range(3))

In 3rd line, it is given : x[i] = x[i], but it doesn’t quite make sense. When I tried running the same code in Python 3 (with some syntax correction), it showed the following error:

def my_function(x):
  for i in range(0, len(x)):
    x[i] = x[i]
  return x

print (my_function(range(3))
TypeError: 'range' object does not support item assignment

Shouldn’t it be “x[i] = i” ?

In Python 2, a range is returned as a list. Lists support item assignment.

In Python 3, a range is returned as an iterable, but is not a list, and so doesn’t support item assignment (there are no items).

The returned range object is actually just a set of constraints that are followed to yield a next value in the range.


@mtf , thanks for clearing my doubt.

1 Like

Thanks for the thread and thanks @mtf for the explanation. I was doing the same exercise of Python 2 but using an IDE that runs Python 3 on my own computer. I had the same error and could not figure out why until I checked here.

For my own test code, I used 2 ways to compare:

def my_function(x):
    for i in range(0, len(x)):
        x[i] = x[i]
    return x

list_1 = [0, 1, 2]
print("Using List as x")

list_2 = range(3)
print("Using Range as x")

I could see that list_1 work but not the list_2 i.e. range() does not behave like a list. mtf’s explanation makes perfect sense why that is.

1 Like