Questions regarding a list iteration mystery


#1

Please take a look at the list and code below. When executed with d[3], d[4], d[5], d[6], d[7], d[8] and d[9] the code prints the entire list once:

d = [1, 2, 3, "A", "B", "C", "D", "E", "F", "G"]

for i in d[3]:
  print d

I need help with the following:

-No list is printed when executed with d[2] or d[1] . Instead I get this error message:
TypeError: ‘int’ object is not iterable

Why is no list printed and what causes the error message?

-Why is the list printed just once when the code is when executed with d[3], d[4], d[5], d[6], d[7], d[8] and d[9]? In this case, what do the indices represent? An index, a value stored at that index, something completely different?

Your help in understating the matter would be appreciated.


#2

That’s because you are telling it to print the list:

We would not use a loop to iterate over a value.

for i in d:
    print i

will iterate over the entire list and print each value.


#3

Hi :slight_smile:

I understand the that the code asks to print the list but what do the indices specifically represent in this use case? An index, a value stored at that index, something completely different?

I’m using print d as a specific use case.


#4
for i in range(len(d)):
    print d[i]

In the above loop we iterate of a range, which values are indexes of d.

In the earlier example, i is a value in the list.


#5

Thank you for the explanation.

I have two last question for which I will use four examples.

1) This code prints the values in list d one after the other, as expected, regardless if it is an interger or a string that is stored at the index location being evaluated :

d = [1, 2, 3, "A", "B", "5", "!"]

for i in d:
  print i

1
2
3
A
B
5
!

2) This code prints specifically the value stored at d[4] which is “B”:

d = [1, 2, 3, "A", "B", "5", "!"]

for i in d[4]:
  print i

B

3) For this code, I thought that it would print the value stored at index d[2] which I thought was 3. However, it returns the error TypeError: ‘int’ object is not iterable:

d = [1, 2, 3, "A", "B", "5", "!"]

for i in d[2]:
  print i

My first question is, why is it that in this code example I get the message that an interger is not iterable when in the first code example it does iterate over interger values and string values?

4) Last but not least, with this code the list prints once as long as the index points to one of the strings. But if it points to an index where an interger is located I get the error TypeError: ‘int’ object is not iterable :

d = [1, 2, 3, "A", "B", "5", "!"]

for i in d[4]:
  print d

[1, 2, 3, ‘A’, ‘B’, ‘5’, ‘!’]

My second question is, why does it print one time as long as the index points to one of the strings?

I really appreciate your help!


#6

That is the ‘iinteger is not iterable’ cause.

>>> d = [1, 2, 3, "A", "B", "5", "!"]
>>> d[2]
3
>>>

We cannot iterate over the number 3. It is a primitive.

Simple. Strings are iterable. A single character means the string has a length, and index[0] is defined.


#7

I thank you for taking the time to answer my questions :slight_smile:
Now I understand better.

I never heard of primitives data types. I briefly looked into it in the context of computer science. It’s really interesting!

Thanks again and take care!


#8

The above is not entirely accurate. In Python there are no primitives the way there are in JavaScript.

number
string
boolean
undefined
null

We can see that in JS terms, strings are primitives. ES6 adds one more to the above list, symbol. But since Python has no primitives, the earlier statement is teetering on the edge, at best.

However, we did make the point about which object was iterable and which was not, the main assertion still holds. Glad you took the time to read up on this. Time well spent, to be sure.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.