Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python range()

Could somebody explain what the range() function is doing here? I understand everything and I passed the challenge with this but I am not sure as to why I need to use range here?

def combo(itr1,itr2):
  tuple_list= []
  for index in range(len(itr1)):
    tuple_list.append((itr1[index],itr2[index]))
  return tuple_list

Thanks!

1 Answer

Hi Nils, The range function returns a range object that essentially is a type of iterable that can be used. For e.g.

x = list(range(5))
# x is [0, 1, 2, 3, 4]

range expects a number to decide how big the iterable will be and it also takes other params that can be seen if you type help(range). In your example:

for index in range(len(itr1)):
# len is evaluating the length of the list and returning it to the range function
# range is then receiving an integer that is the size of the list and creates an iterable
# that has the values starting from 0 up until the len of the list. e.g. [0, 1, 2, ...., len(itr1) -1]

If you have any further questions leave a comment! Hope this helps! Happy programming Nils! :) -CodyTheCoder