“TypeError: 'int' object is not iterable” When making a small words list
I need to make a function using iteration to make a list of all words that are shorter than 3 letters. I keep getting some int error.
def shortWords(aList):
total = 0
aList = 0
for index in aList:
index = str(index)
if len(aList([index])) <= 3:
total = aList.append([index])
return total
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
python
add a comment |
I need to make a function using iteration to make a list of all words that are shorter than 3 letters. I keep getting some int error.
def shortWords(aList):
total = 0
aList = 0
for index in aList:
index = str(index)
if len(aList([index])) <= 3:
total = aList.append([index])
return total
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
python
1
aList = 0
seems to destroy your list, it looks like irrelevant statement.
– muradm
Nov 13 '18 at 2:28
add a comment |
I need to make a function using iteration to make a list of all words that are shorter than 3 letters. I keep getting some int error.
def shortWords(aList):
total = 0
aList = 0
for index in aList:
index = str(index)
if len(aList([index])) <= 3:
total = aList.append([index])
return total
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
python
I need to make a function using iteration to make a list of all words that are shorter than 3 letters. I keep getting some int error.
def shortWords(aList):
total = 0
aList = 0
for index in aList:
index = str(index)
if len(aList([index])) <= 3:
total = aList.append([index])
return total
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
python
python
edited Nov 13 '18 at 23:30
Terry Jan Reedy
11.9k12140
11.9k12140
asked Nov 13 '18 at 0:47
APCSPAPCSP
12
12
1
aList = 0
seems to destroy your list, it looks like irrelevant statement.
– muradm
Nov 13 '18 at 2:28
add a comment |
1
aList = 0
seems to destroy your list, it looks like irrelevant statement.
– muradm
Nov 13 '18 at 2:28
1
1
aList = 0
seems to destroy your list, it looks like irrelevant statement.– muradm
Nov 13 '18 at 2:28
aList = 0
seems to destroy your list, it looks like irrelevant statement.– muradm
Nov 13 '18 at 2:28
add a comment |
2 Answers
2
active
oldest
votes
You got TypeError: 'int' object is not iterable
because you have declared aList = 0
in your function, which overwrites the parameter that is passed into it.
It seems there is some confusion on how python loops work. When iterating through a list, it will return the value and not the index. So you can use it directly.
def shortWords(aList):
words = # Declared to store any short words found
for word in aList:
if len(word) <= 3:
words.append(word) # Short word found, appending to list
return words # Return results
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
# ['my', 'is']
Thank you! Why did you assign the words to the empty brackets? Also, why did you add the underscore between the short words?
– APCSP
Nov 13 '18 at 1:02
whoops sorry, that was a mistake. That was just supposed to bewords
. The empty brackets (words =
) indicates a declaration of an empty list data type. Since the output was for a list of short words, the correct data type should be declared to contain it.
– boonwj
Nov 13 '18 at 1:06
How do we know that "words" is assigned to the new list that needs to be appended?
– APCSP
Nov 13 '18 at 1:15
I have modified the code snippet with comments to explain the flow
– boonwj
Nov 13 '18 at 1:34
add a comment |
Because you have a:
aList = 0
In the second line of the code!
Which overwrites the actual list.
So should remove it, then it will work, so would be:
def shortWords(aList):
words =
for i in aList:
if len(i) <= 3:
words.append(i)
return words
Note that it aList
doesn't contain the indexes, but the values already, remove few steps
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272184%2ftypeerror-int-object-is-not-iterable-when-making-a-small-words-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You got TypeError: 'int' object is not iterable
because you have declared aList = 0
in your function, which overwrites the parameter that is passed into it.
It seems there is some confusion on how python loops work. When iterating through a list, it will return the value and not the index. So you can use it directly.
def shortWords(aList):
words = # Declared to store any short words found
for word in aList:
if len(word) <= 3:
words.append(word) # Short word found, appending to list
return words # Return results
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
# ['my', 'is']
Thank you! Why did you assign the words to the empty brackets? Also, why did you add the underscore between the short words?
– APCSP
Nov 13 '18 at 1:02
whoops sorry, that was a mistake. That was just supposed to bewords
. The empty brackets (words =
) indicates a declaration of an empty list data type. Since the output was for a list of short words, the correct data type should be declared to contain it.
– boonwj
Nov 13 '18 at 1:06
How do we know that "words" is assigned to the new list that needs to be appended?
– APCSP
Nov 13 '18 at 1:15
I have modified the code snippet with comments to explain the flow
– boonwj
Nov 13 '18 at 1:34
add a comment |
You got TypeError: 'int' object is not iterable
because you have declared aList = 0
in your function, which overwrites the parameter that is passed into it.
It seems there is some confusion on how python loops work. When iterating through a list, it will return the value and not the index. So you can use it directly.
def shortWords(aList):
words = # Declared to store any short words found
for word in aList:
if len(word) <= 3:
words.append(word) # Short word found, appending to list
return words # Return results
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
# ['my', 'is']
Thank you! Why did you assign the words to the empty brackets? Also, why did you add the underscore between the short words?
– APCSP
Nov 13 '18 at 1:02
whoops sorry, that was a mistake. That was just supposed to bewords
. The empty brackets (words =
) indicates a declaration of an empty list data type. Since the output was for a list of short words, the correct data type should be declared to contain it.
– boonwj
Nov 13 '18 at 1:06
How do we know that "words" is assigned to the new list that needs to be appended?
– APCSP
Nov 13 '18 at 1:15
I have modified the code snippet with comments to explain the flow
– boonwj
Nov 13 '18 at 1:34
add a comment |
You got TypeError: 'int' object is not iterable
because you have declared aList = 0
in your function, which overwrites the parameter that is passed into it.
It seems there is some confusion on how python loops work. When iterating through a list, it will return the value and not the index. So you can use it directly.
def shortWords(aList):
words = # Declared to store any short words found
for word in aList:
if len(word) <= 3:
words.append(word) # Short word found, appending to list
return words # Return results
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
# ['my', 'is']
You got TypeError: 'int' object is not iterable
because you have declared aList = 0
in your function, which overwrites the parameter that is passed into it.
It seems there is some confusion on how python loops work. When iterating through a list, it will return the value and not the index. So you can use it directly.
def shortWords(aList):
words = # Declared to store any short words found
for word in aList:
if len(word) <= 3:
words.append(word) # Short word found, appending to list
return words # Return results
print(shortWords(['Hello', 'my', 'name', 'is', 'Inigo', 'Montoya']))
# ['my', 'is']
edited Nov 13 '18 at 1:33
answered Nov 13 '18 at 0:51
boonwjboonwj
2169
2169
Thank you! Why did you assign the words to the empty brackets? Also, why did you add the underscore between the short words?
– APCSP
Nov 13 '18 at 1:02
whoops sorry, that was a mistake. That was just supposed to bewords
. The empty brackets (words =
) indicates a declaration of an empty list data type. Since the output was for a list of short words, the correct data type should be declared to contain it.
– boonwj
Nov 13 '18 at 1:06
How do we know that "words" is assigned to the new list that needs to be appended?
– APCSP
Nov 13 '18 at 1:15
I have modified the code snippet with comments to explain the flow
– boonwj
Nov 13 '18 at 1:34
add a comment |
Thank you! Why did you assign the words to the empty brackets? Also, why did you add the underscore between the short words?
– APCSP
Nov 13 '18 at 1:02
whoops sorry, that was a mistake. That was just supposed to bewords
. The empty brackets (words =
) indicates a declaration of an empty list data type. Since the output was for a list of short words, the correct data type should be declared to contain it.
– boonwj
Nov 13 '18 at 1:06
How do we know that "words" is assigned to the new list that needs to be appended?
– APCSP
Nov 13 '18 at 1:15
I have modified the code snippet with comments to explain the flow
– boonwj
Nov 13 '18 at 1:34
Thank you! Why did you assign the words to the empty brackets? Also, why did you add the underscore between the short words?
– APCSP
Nov 13 '18 at 1:02
Thank you! Why did you assign the words to the empty brackets? Also, why did you add the underscore between the short words?
– APCSP
Nov 13 '18 at 1:02
whoops sorry, that was a mistake. That was just supposed to be
words
. The empty brackets (words =
) indicates a declaration of an empty list data type. Since the output was for a list of short words, the correct data type should be declared to contain it.– boonwj
Nov 13 '18 at 1:06
whoops sorry, that was a mistake. That was just supposed to be
words
. The empty brackets (words =
) indicates a declaration of an empty list data type. Since the output was for a list of short words, the correct data type should be declared to contain it.– boonwj
Nov 13 '18 at 1:06
How do we know that "words" is assigned to the new list that needs to be appended?
– APCSP
Nov 13 '18 at 1:15
How do we know that "words" is assigned to the new list that needs to be appended?
– APCSP
Nov 13 '18 at 1:15
I have modified the code snippet with comments to explain the flow
– boonwj
Nov 13 '18 at 1:34
I have modified the code snippet with comments to explain the flow
– boonwj
Nov 13 '18 at 1:34
add a comment |
Because you have a:
aList = 0
In the second line of the code!
Which overwrites the actual list.
So should remove it, then it will work, so would be:
def shortWords(aList):
words =
for i in aList:
if len(i) <= 3:
words.append(i)
return words
Note that it aList
doesn't contain the indexes, but the values already, remove few steps
add a comment |
Because you have a:
aList = 0
In the second line of the code!
Which overwrites the actual list.
So should remove it, then it will work, so would be:
def shortWords(aList):
words =
for i in aList:
if len(i) <= 3:
words.append(i)
return words
Note that it aList
doesn't contain the indexes, but the values already, remove few steps
add a comment |
Because you have a:
aList = 0
In the second line of the code!
Which overwrites the actual list.
So should remove it, then it will work, so would be:
def shortWords(aList):
words =
for i in aList:
if len(i) <= 3:
words.append(i)
return words
Note that it aList
doesn't contain the indexes, but the values already, remove few steps
Because you have a:
aList = 0
In the second line of the code!
Which overwrites the actual list.
So should remove it, then it will work, so would be:
def shortWords(aList):
words =
for i in aList:
if len(i) <= 3:
words.append(i)
return words
Note that it aList
doesn't contain the indexes, but the values already, remove few steps
answered Nov 13 '18 at 1:10
U9-ForwardU9-Forward
13.6k21337
13.6k21337
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272184%2ftypeerror-int-object-is-not-iterable-when-making-a-small-words-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
aList = 0
seems to destroy your list, it looks like irrelevant statement.– muradm
Nov 13 '18 at 2:28