Python extend method for lists decompose elements of the first list in to the second
Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?
a_list = ['2', '3m7n', '3', '17', None]
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']
enter image description here
python loops
add a comment |
Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?
a_list = ['2', '3m7n', '3', '17', None]
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']
enter image description here
python loops
Can you please paste the code as text in your question? It is hard people to help with an image of your code.
– slider
Nov 26 '18 at 4:00
Useappend
instead ofextend
. stackoverflow.com/questions/252703/…
– slider
Nov 26 '18 at 4:04
add a comment |
Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?
a_list = ['2', '3m7n', '3', '17', None]
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']
enter image description here
python loops
Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?
a_list = ['2', '3m7n', '3', '17', None]
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']
enter image description here
python loops
python loops
edited Nov 27 '18 at 16:23
Ramon
asked Nov 26 '18 at 3:57
RamonRamon
156
156
Can you please paste the code as text in your question? It is hard people to help with an image of your code.
– slider
Nov 26 '18 at 4:00
Useappend
instead ofextend
. stackoverflow.com/questions/252703/…
– slider
Nov 26 '18 at 4:04
add a comment |
Can you please paste the code as text in your question? It is hard people to help with an image of your code.
– slider
Nov 26 '18 at 4:00
Useappend
instead ofextend
. stackoverflow.com/questions/252703/…
– slider
Nov 26 '18 at 4:04
Can you please paste the code as text in your question? It is hard people to help with an image of your code.
– slider
Nov 26 '18 at 4:00
Can you please paste the code as text in your question? It is hard people to help with an image of your code.
– slider
Nov 26 '18 at 4:00
Use
append
instead of extend
. stackoverflow.com/questions/252703/…– slider
Nov 26 '18 at 4:04
Use
append
instead of extend
. stackoverflow.com/questions/252703/…– slider
Nov 26 '18 at 4:04
add a comment |
2 Answers
2
active
oldest
votes
extend
expects a list
append
expects an element
a_list = ['2', '3m7n', '3', '17']
b_list = ['bat', 'zoo', 'next']
b_list.extend([i for i in a_list if len(i) == 4 ])
print(b_list)
add a comment |
append
adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend
adds each element in an iterable to the end of the list.
The mistake you are making is extending a list with a str
sequence, if you want to add an object use append:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.append(i)
b
['bat', 'zoo', 'next', '3', 'm', '7', 'n']
The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i)
. Using the same context, but converting each item in the loop to a list
, you use extend to add each item in the list:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.extend([i])
b
>>['bat', 'zoo', 'next', '3m7n']
For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for
loop:
b.extend([i for i in a if len(i) == 4 ])
And if you observe, it works exactly the same as the for loop, by extending the list with a list object.
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%2f53474612%2fpython-extend-method-for-lists-decompose-elements-of-the-first-list-in-to-the-se%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
extend
expects a list
append
expects an element
a_list = ['2', '3m7n', '3', '17']
b_list = ['bat', 'zoo', 'next']
b_list.extend([i for i in a_list if len(i) == 4 ])
print(b_list)
add a comment |
extend
expects a list
append
expects an element
a_list = ['2', '3m7n', '3', '17']
b_list = ['bat', 'zoo', 'next']
b_list.extend([i for i in a_list if len(i) == 4 ])
print(b_list)
add a comment |
extend
expects a list
append
expects an element
a_list = ['2', '3m7n', '3', '17']
b_list = ['bat', 'zoo', 'next']
b_list.extend([i for i in a_list if len(i) == 4 ])
print(b_list)
extend
expects a list
append
expects an element
a_list = ['2', '3m7n', '3', '17']
b_list = ['bat', 'zoo', 'next']
b_list.extend([i for i in a_list if len(i) == 4 ])
print(b_list)
answered Nov 26 '18 at 4:06
KrishnaKrishna
6021515
6021515
add a comment |
add a comment |
append
adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend
adds each element in an iterable to the end of the list.
The mistake you are making is extending a list with a str
sequence, if you want to add an object use append:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.append(i)
b
['bat', 'zoo', 'next', '3', 'm', '7', 'n']
The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i)
. Using the same context, but converting each item in the loop to a list
, you use extend to add each item in the list:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.extend([i])
b
>>['bat', 'zoo', 'next', '3m7n']
For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for
loop:
b.extend([i for i in a if len(i) == 4 ])
And if you observe, it works exactly the same as the for loop, by extending the list with a list object.
add a comment |
append
adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend
adds each element in an iterable to the end of the list.
The mistake you are making is extending a list with a str
sequence, if you want to add an object use append:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.append(i)
b
['bat', 'zoo', 'next', '3', 'm', '7', 'n']
The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i)
. Using the same context, but converting each item in the loop to a list
, you use extend to add each item in the list:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.extend([i])
b
>>['bat', 'zoo', 'next', '3m7n']
For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for
loop:
b.extend([i for i in a if len(i) == 4 ])
And if you observe, it works exactly the same as the for loop, by extending the list with a list object.
add a comment |
append
adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend
adds each element in an iterable to the end of the list.
The mistake you are making is extending a list with a str
sequence, if you want to add an object use append:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.append(i)
b
['bat', 'zoo', 'next', '3', 'm', '7', 'n']
The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i)
. Using the same context, but converting each item in the loop to a list
, you use extend to add each item in the list:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.extend([i])
b
>>['bat', 'zoo', 'next', '3m7n']
For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for
loop:
b.extend([i for i in a if len(i) == 4 ])
And if you observe, it works exactly the same as the for loop, by extending the list with a list object.
append
adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend
adds each element in an iterable to the end of the list.
The mistake you are making is extending a list with a str
sequence, if you want to add an object use append:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.append(i)
b
['bat', 'zoo', 'next', '3', 'm', '7', 'n']
The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i)
. Using the same context, but converting each item in the loop to a list
, you use extend to add each item in the list:
a = ['2', '3m7n', '3', '17']
b = ['bat', 'zoo', 'next']
for i in a:
if len(i) == 4:
b.extend([i])
b
>>['bat', 'zoo', 'next', '3m7n']
For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for
loop:
b.extend([i for i in a if len(i) == 4 ])
And if you observe, it works exactly the same as the for loop, by extending the list with a list object.
answered Nov 26 '18 at 5:21
BernardLBernardL
2,40811130
2,40811130
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.
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%2f53474612%2fpython-extend-method-for-lists-decompose-elements-of-the-first-list-in-to-the-se%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
Can you please paste the code as text in your question? It is hard people to help with an image of your code.
– slider
Nov 26 '18 at 4:00
Use
append
instead ofextend
. stackoverflow.com/questions/252703/…– slider
Nov 26 '18 at 4:04