1

Here is the code I wrote:

a = "5,3,1"
print (type(a))
b = []
b = a.split(",")
print (type(b))
print (b)
for i in b:
    if (int(i) % 5 == 0):
        print (i) 

print (type(i))

In the for loop, I was expecting the variable i to be an integer type. But turns out that it is a string. How does this make sense?

Output

<class 'str'>                                                                                                                                                                      
<class 'list'>                                                                                                                                                                     
['5', '3', '1']                                                                                                                                                                    
5                                                                                                                                                                                  
<class 'str'> 

How can we use a string as an iterator to loop through the list items in a list?
Further if I remove the int in the line if (int(i) % 5 == 0):, I get this error:
TypeError: not all arguments converted during string formatting
I'm unable to understand how this works

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Prasanna
  • 303
  • 2
  • 16
  • 1
    Why do you expect it to be an `int`? `b == a.split()` and `str.split` always returns a list of strings, so of course when you iterate over a list of strings, the items are strings... Note, `i` **is not an iterator** in Python terminology. `i` is simply a variable that gets assigned the next item in the iterator returned by the iterable you are iterating over – juanpa.arrivillaga Apr 23 '20 at 18:48
  • Excuse my poor knowledge. `For` loops normally have iterators in other languages... Looks like it is different in python – Prasanna Apr 23 '20 at 18:51
  • 1
    [This answer in particular](https://stackoverflow.com/a/57816761/5014455) which goes into detail and addresses confusions that may come from other languages – juanpa.arrivillaga Apr 23 '20 at 18:51
  • 1
    Python does use iterator-based for-loops. But `i` is not an iterator, it is merely a variable that gets assigned *the next item from the iterator* on each iteration. What language did you have in mind? *why* did you expect `i` to be an `int`? What ints were you expecting? – juanpa.arrivillaga Apr 23 '20 at 18:52
  • 1
    @juanpa.arrivillaga thanks for your reference. It's now clear to me – Prasanna Apr 23 '20 at 18:56

2 Answers2

1

When you split a string into b, you get a list of strings, ["5", "3", "1"]. When you iterate through b, your loop variable, i is assigned to each value in the list, and each value is a string. You can turn the value in i into an integer with int(i), but unless you manually convert the value, it remains a string. This is why you cannot use if i % 5, because string % int is not defined in Python.

Nathan Collins
  • 301
  • 2
  • 5
1

b is a list of strings, correct? This is b:

['5', '3', '1']

So when you start iterating through b, each value of i will be a string because strings are all there is in b. When you say int(i) in your if statement, you are not changing i to an integer. You are only getting the integer value of i.

If you want b to be a list of integers instead of a list of strings, use this instead:

b = [int(num) for num in a.split(",")]

Output:

<class 'str'>
<class 'list'>
[5, 3, 1]
5
<class 'int'>

You can see that i is now an integer.

Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28