how to loop through a list of integers
Question:
I’m trying to loop through a list of integers by using a for loop. However, we know that in python, int is not iterable. So I’m wondering if there’s any ways I can store integers in another way so that I can loop through the integers?
def get_index_of_smallest(pint):
integers_list = []
for int in pint:
if int > 0:
integers_list.append(int)
return min(integers_list)
integers = int(input("Please enter a bunch of integers: "))
get_index_of_smallest(integers)
The code above is what I’m trying to attempt. You see that if I try to pass a list of integers into a for loop, python returns with an error and saying the data type "int" is not iterable.
Answers:
Python is trying to change your input to integer and as you know you can’t iterate through integers so for your problem string.split(separator, maxsplit)
would be perfect.
This function separates the string into several parts according to the separator you gave to it (in your example space ' '
). You can change all the values in new list to integers with a list comprehension, such as new_list = [int(x) for x in integers]
.
Your final code will look like this:
def get_index_of_smallest(array):
array = [int(x) for x in array.split(' ') if x > 0]
return min(array)
integers = input("Please enter a bunch of integers separated by space: ")
get_index_of_smallest(integers)
Try something like this:
user_input = input("Please enter a bunch of integers: ")
number_list = user_input.split(" ") # or a different delimeter
integers = [int(entry) for entry in number_list]
get_index_of_smallest(integers)
You can modify your code like, for taking the input rather than converting everything to int
itself you can use map
:
def get_index_of_smallest(pint):
m=float('inf')
least_ind=-1
for i in range(len(pint)):
if pint[i]<m:
least_ind=i
m=pint[i]
return least_ind
integers = input("Please enter a bunch of integers: ")
integers = list(map(int,integers.split()))
get_index_of_smallest(integers)
I have modified your code and separated each step for better understanding.
There seems to be a few things you may want to fix in your code:
- Do not use
int
as variable name. It has a special meaning in Python and this name should never be used in other ways.
- If you expect user to input a bunch of numbers you cannot directly cast input to int. It depends on how the numbers will be inputed but you first need to separate them from input string (initially
input
returns strings). That’s what’s causing your error. Look at the previous answers for examples.
- The logic of your function could use some improvement. If you want to return smallest, non-negative element from your list there is no need to first filter them to separate list.
def get_index_of_smallest(pint):
integers_list = []
for integer in pint:
if int(integer) > 0:
integers_list.append(int(integer))
return min(integers_list)
integers = int(input("Please enter a bunch of integers: "))
print(get_index_of_smallest(str(integers)))
You could try this, converting the input integers into a string directly.
I’m trying to loop through a list of integers by using a for loop. However, we know that in python, int is not iterable. So I’m wondering if there’s any ways I can store integers in another way so that I can loop through the integers?
def get_index_of_smallest(pint):
integers_list = []
for int in pint:
if int > 0:
integers_list.append(int)
return min(integers_list)
integers = int(input("Please enter a bunch of integers: "))
get_index_of_smallest(integers)
The code above is what I’m trying to attempt. You see that if I try to pass a list of integers into a for loop, python returns with an error and saying the data type "int" is not iterable.
Python is trying to change your input to integer and as you know you can’t iterate through integers so for your problem string.split(separator, maxsplit)
would be perfect.
This function separates the string into several parts according to the separator you gave to it (in your example space ' '
). You can change all the values in new list to integers with a list comprehension, such as new_list = [int(x) for x in integers]
.
Your final code will look like this:
def get_index_of_smallest(array):
array = [int(x) for x in array.split(' ') if x > 0]
return min(array)
integers = input("Please enter a bunch of integers separated by space: ")
get_index_of_smallest(integers)
Try something like this:
user_input = input("Please enter a bunch of integers: ")
number_list = user_input.split(" ") # or a different delimeter
integers = [int(entry) for entry in number_list]
get_index_of_smallest(integers)
You can modify your code like, for taking the input rather than converting everything to int
itself you can use map
:
def get_index_of_smallest(pint):
m=float('inf')
least_ind=-1
for i in range(len(pint)):
if pint[i]<m:
least_ind=i
m=pint[i]
return least_ind
integers = input("Please enter a bunch of integers: ")
integers = list(map(int,integers.split()))
get_index_of_smallest(integers)
I have modified your code and separated each step for better understanding.
There seems to be a few things you may want to fix in your code:
- Do not use
int
as variable name. It has a special meaning in Python and this name should never be used in other ways. - If you expect user to input a bunch of numbers you cannot directly cast input to int. It depends on how the numbers will be inputed but you first need to separate them from input string (initially
input
returns strings). That’s what’s causing your error. Look at the previous answers for examples. - The logic of your function could use some improvement. If you want to return smallest, non-negative element from your list there is no need to first filter them to separate list.
def get_index_of_smallest(pint):
integers_list = []
for integer in pint:
if int(integer) > 0:
integers_list.append(int(integer))
return min(integers_list)
integers = int(input("Please enter a bunch of integers: "))
print(get_index_of_smallest(str(integers)))
You could try this, converting the input integers into a string directly.