Should I be using range in my for loops?

Hi. Could someone please explain why this code results in a SyntaxError?

def exponents(bases, powers):
	new_lst = []
	for i in range(len(powers)):
		i = range(len(bases[0:]) ** powers[i]
		new_lst.append(i)
	return new_lst

When you see a syntax error in Python thrown on a line that you know to be correct, look to the preceding line for a missing close-quote or close-parentheses.

Whoops. Thanks for the help. Fixed the mistake, but now I’m getting a “TypeError: unsupported operand type(s) for ** or pow(): ‘range’ and ‘int’”.

def exponents(bases, powers):
	new_lst = []
	for i in range(len(powers)):
		i = range(len(bases[0:])) ** powers[i]
		new_lst.append(i)
	return new_lst

What you want to happen in the first line of the ‘for’ block isn’t going to work. range() does not work that way. I think you mean for every integer in the range() to be raised to a power. Although you could find some functionality close to that using NumPy, it won’t work in native Python. You will need to retrieve each item in range() (just as you do in the ‘for’ loop) to work with individually.

Hint 1: You’ll need a nested ‘for’ loop.

Hint 2: Because of Python’s magic ‘in’ operator, you don’t need any indexes (or range()) at all!

lst = ['a', 'b', 'c']

for idx in range(len(lst)):
    print(lst[idx], end = " ")
    
print()

for item in lst:        # no indexes!
    print(item, end = " ")

# Output:
a b c 
a b c
1 Like

Ah, got ya. My original solution was:

def exponents(bases, powers):
	new_lst = []
	for b in range(len(bases)):
		for p in range(len(powers)):
			result = bases[b] ** powers[p]
			new_lst.append(result)
	return new_lst

However, I can see that some of the code is redundant:

def exponents(bases, powers):
	new_lst = []
	for b in bases:
		for p in powers:
			result = b ** p
			new_lst.append(result)
	return new_lst

Cheers!

Or given to using the index for read-only purposes. We only need to use this functionality if the intent is to be able to modify the value of list items. No harm in writing code that way, as long as we see what it is doing… Giving us write permission.

The technical difference is that one, in range() uses the members in a range sequence as indices for accessing values; whereas, the other in sequence uses the members of the sequence as read-only values taken directly from the list.

There is also a difference in which methods are used in the background but I’m not the best at describing that much detail so will defer to better information sources than the generalized picture we can give here.

From a practical view, if all we wish our function to do is read the sequences we give them, the in sequence is the way to code that particular function.

Whatever your decision, do not code one way or another because someone told you it is better. Write however you wish and discover for one’s self what might be more suitable to a given situation. In other words, and I hate to say this, don’t listen to the don’ters unless you clearly understand and agree with what they’re saying not to do. Otherwise ignore and keep on coding as you would given whatever the prevailing circumstances.

Right. The truth is that I did not actually know why the suggestion made by @patrickd314 was a superior one, other than his code being shorter and therefore supposedly more efficient.

It is becoming clear to me that one can do something well without actually understanding what it is that he is doing, at a core level. I have heard a similar sentiment towards those who graduate a 4 month intensive coding boot-camp, when compared to CS graduates who have gotten to grips with the intensive maths that is fundamental to software engineering.

1 Like