Why would we want to print index + 1?

Question

Why would we want to print index + 1?

Answer

Often at times when you write code, it makes sense to you as a coder. However, things like counting from 0 are not common elsewhere! Someone reading your menu of options might be put off by 0 being an option, and the remaining options seeming to be less than their position by one.
To account for this, adjusting your output values that are visible to the user is a common practice. It keeps your internal code readable by you and other coders, and the output pleasant for everyone else to look at!

5 Likes

I have no idea how to do this one…here is what I have tried:

d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
  enumerate d[key]
3 Likes

I have just refreshed the page and it has now come up with a completely different set of code:

choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:'
for index, item in enumerate(choices):
  print index + 1, item

Nevertheless, does the ‘index, item’ syntax access the indexes of all the items? That is, is this the generic syntax for accessing the index of an item? an alternative to list[item]?

What is the goal (objective) in your code?

>>> d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}
>>> for key in d:
	print (list(enumerate(d[key])))

	
[(0, 'a'), (1, 'p'), (2, 'p'), (3, 'l'), (4, 'e')]
[(0, 'b'), (1, 'e'), (2, 'r'), (3, 'r'), (4, 'y')]
[(0, 'c'), (1, 'h'), (2, 'e'), (3, 'r'), (4, 'r'), (5, 'y')]
>>> 

Notice how the strings in d have been enumerated? Is that the desired outcome?

We can use the enumerate to access both index and value in one go.

>>> for index, item in enumerate(choices):
  print (index + 1, item)

  
1 pizza
2 pasta
3 salad
4 nachos
>>> 

The idea of adding 1 to the index gives us natural counting numbers. Not many restaurants have a Number Zero on the menu.

2 Likes

Ah okay, so do you have to put:

for index, item in enumerate(choices):

Or can you just put:

for item in enumerate(choices):

?

3 Likes

The enumerate() function needs both variables order to access the item as a printable value. If we leave off the index variable, it prints the values as tuples with index and quoted strings, which may not be what we wish to display.

>>> for item in enumerate(choices):
  print (item)

  
(0, 'pizza')
(1, 'pasta')
(2, 'salad')
(3, 'nachos')
>>> 

Now ask one’s self, does it make sense to use a function in a non-standard way?

>>> for item in choices:
	print (item)

	
pizza
pasta
salad
nachos
>>> 
2 Likes

Ah okay, so this is just how the ‘enumerate’ function works?

Non-standard way?

1 Like

Writing for item in enumerate(choices) is not a standard usage. If all we want is the item, then,

for item in choices:
2 Likes

Got it!

Thanks a lot!

3 Likes

In this problem, we are to print a numbered list with the item starting with index 1 instead of 0. Why would I use print index + 1, instead of print index(range(1))?

choices = [‘pizza’, ‘pasta’, ‘salad’, ‘nachos’]
for index, item in enumerate(choices):
print index + 1, item

Console display:
Your choices are:

1 pizza

2 pasta

3 salad

4 nachos

Because index has a value that is just one less than the desired output in all instances. No need to introduce more logic.

Consider also,

>>> range(1)
range(0, 1)
>>> [*range(1)]
[0]
>>> 

Note that range(1) will cast to a list with the single value, 0.

Thank you, Mtf, you are so good to reply quickly and to suggest so many things to think about. You truly must want to be helpful. This has been mind stretching for me, and your support gives me hope for success.

From your reply, I understand the simplicity of index + 1. In your consider also section, what are the different results of range(1), range (0,1), [*range(1)], [0]? When you say "range (1) will cast (what does cast mean) to a list with the single value, 0, what does this look like?

When I complete these tutorials, I almost always end up having the solution given to me. I really try several times first on my own. Once the solution is there, I wish I had someone to talk through each bit of code and tell me what it means.

Thanks!

1 Like

It means assign a type, in this case a list. A range object is not a list, but may be cast to a list so we can observe the values. We can iterate a range but we cannot display it in one command without first making it a list (or a tuple).

As we can see above, it looks like,

[0]

a list with len() == 1, and value at index 0 == 0.

Above, when you see, >>> that is the prompt of the interactive console. It is a Python command line. Only the values without the carats in front are results. range(0, 1) and [0], respectively.

Hmm … thanks. I’ll think on these ideas.

1 Like

Hi Mtf,

It looks like the forum about factorials has been closed.

Will you explain how the *= and -= in this code? I understand the definitions of these operants, but struggle with the math.

ef factorial(x):
total = 1
while x>0:
total *= x
x-=1
return total

print factorial(4)

*= is an augmentation assignment operator for multiplication

a = 5
a *= 5
print (a)    #  25

It is a shorthand form of,

a = a * 5

only with one difference, it is in-place, operating on the number on the left.

-= is a diminishing assignment operator for subtraction.

a = 13
a -= 7
print (a)    #  6

similar to,

a = a - 7

The expression on the right is evaluated, then assigned back to the same variable.

1 Like