Python list() function on (non) iterable
I'm facing an issue with the list() function. I have a simple class
class Foo(object):
def __init__(self, value):
self.value = value
And I'm trying to construct a list with the Foo object:
lhs = list(Foo(1))
...
rhs = list(Foo(2))
return lhs + rhs
The desired result should be [Foo(1), Foo(2)]. However I need to use such result in the recursive call, so in the following step this result becomes the argument of the list() function of lhs and I want to append such list by another Foo element:
lhs = list([Foo(1), Foo(2)])
...
rhs = list(Foo(3))
return lhs + rhs
And the final result should be [Foo(1), Foo(2), Foo(3)]. The issue is that the list(Foo(1)) function raises the TypeError because the Foo is not iterable. Is there any way how to fix it?
EDIT
A better use case:
def create_foo(value):
return Foo(value)
def concatenate(lhs, rhs):
return list(lhs) + list(rhs)
concatenate(concatenate(create_foo(1), create_foo(2)), create_foo(3))
python-3.x list
add a comment |
I'm facing an issue with the list() function. I have a simple class
class Foo(object):
def __init__(self, value):
self.value = value
And I'm trying to construct a list with the Foo object:
lhs = list(Foo(1))
...
rhs = list(Foo(2))
return lhs + rhs
The desired result should be [Foo(1), Foo(2)]. However I need to use such result in the recursive call, so in the following step this result becomes the argument of the list() function of lhs and I want to append such list by another Foo element:
lhs = list([Foo(1), Foo(2)])
...
rhs = list(Foo(3))
return lhs + rhs
And the final result should be [Foo(1), Foo(2), Foo(3)]. The issue is that the list(Foo(1)) function raises the TypeError because the Foo is not iterable. Is there any way how to fix it?
EDIT
A better use case:
def create_foo(value):
return Foo(value)
def concatenate(lhs, rhs):
return list(lhs) + list(rhs)
concatenate(concatenate(create_foo(1), create_foo(2)), create_foo(3))
python-3.x list
3
list((Foo(1),))? Or just[Foo(1)]?
– jonrsharpe
Jan 18 at 21:30
It sounds like you just need an explicit base case for when you have a single item on the left hand side (or no items returning an empty list, depending what you're doing).
– Patrick Haugh
Jan 18 at 21:32
@jonrsharpe I added a better example. The[Foo(1)]would create nested lists which are not the desired result
– marthin23
Jan 18 at 21:51
1
Why are you trying to pass bothFooinstances and sequences ofFooinstances toconcatenate? That seems like a bad idea.
– user2357112
Jan 19 at 6:20
add a comment |
I'm facing an issue with the list() function. I have a simple class
class Foo(object):
def __init__(self, value):
self.value = value
And I'm trying to construct a list with the Foo object:
lhs = list(Foo(1))
...
rhs = list(Foo(2))
return lhs + rhs
The desired result should be [Foo(1), Foo(2)]. However I need to use such result in the recursive call, so in the following step this result becomes the argument of the list() function of lhs and I want to append such list by another Foo element:
lhs = list([Foo(1), Foo(2)])
...
rhs = list(Foo(3))
return lhs + rhs
And the final result should be [Foo(1), Foo(2), Foo(3)]. The issue is that the list(Foo(1)) function raises the TypeError because the Foo is not iterable. Is there any way how to fix it?
EDIT
A better use case:
def create_foo(value):
return Foo(value)
def concatenate(lhs, rhs):
return list(lhs) + list(rhs)
concatenate(concatenate(create_foo(1), create_foo(2)), create_foo(3))
python-3.x list
I'm facing an issue with the list() function. I have a simple class
class Foo(object):
def __init__(self, value):
self.value = value
And I'm trying to construct a list with the Foo object:
lhs = list(Foo(1))
...
rhs = list(Foo(2))
return lhs + rhs
The desired result should be [Foo(1), Foo(2)]. However I need to use such result in the recursive call, so in the following step this result becomes the argument of the list() function of lhs and I want to append such list by another Foo element:
lhs = list([Foo(1), Foo(2)])
...
rhs = list(Foo(3))
return lhs + rhs
And the final result should be [Foo(1), Foo(2), Foo(3)]. The issue is that the list(Foo(1)) function raises the TypeError because the Foo is not iterable. Is there any way how to fix it?
EDIT
A better use case:
def create_foo(value):
return Foo(value)
def concatenate(lhs, rhs):
return list(lhs) + list(rhs)
concatenate(concatenate(create_foo(1), create_foo(2)), create_foo(3))
python-3.x list
python-3.x list
edited Jan 18 at 21:42
marthin23
asked Jan 18 at 21:27
marthin23marthin23
102210
102210
3
list((Foo(1),))? Or just[Foo(1)]?
– jonrsharpe
Jan 18 at 21:30
It sounds like you just need an explicit base case for when you have a single item on the left hand side (or no items returning an empty list, depending what you're doing).
– Patrick Haugh
Jan 18 at 21:32
@jonrsharpe I added a better example. The[Foo(1)]would create nested lists which are not the desired result
– marthin23
Jan 18 at 21:51
1
Why are you trying to pass bothFooinstances and sequences ofFooinstances toconcatenate? That seems like a bad idea.
– user2357112
Jan 19 at 6:20
add a comment |
3
list((Foo(1),))? Or just[Foo(1)]?
– jonrsharpe
Jan 18 at 21:30
It sounds like you just need an explicit base case for when you have a single item on the left hand side (or no items returning an empty list, depending what you're doing).
– Patrick Haugh
Jan 18 at 21:32
@jonrsharpe I added a better example. The[Foo(1)]would create nested lists which are not the desired result
– marthin23
Jan 18 at 21:51
1
Why are you trying to pass bothFooinstances and sequences ofFooinstances toconcatenate? That seems like a bad idea.
– user2357112
Jan 19 at 6:20
3
3
list((Foo(1),))? Or just [Foo(1)]?– jonrsharpe
Jan 18 at 21:30
list((Foo(1),))? Or just [Foo(1)]?– jonrsharpe
Jan 18 at 21:30
It sounds like you just need an explicit base case for when you have a single item on the left hand side (or no items returning an empty list, depending what you're doing).
– Patrick Haugh
Jan 18 at 21:32
It sounds like you just need an explicit base case for when you have a single item on the left hand side (or no items returning an empty list, depending what you're doing).
– Patrick Haugh
Jan 18 at 21:32
@jonrsharpe I added a better example. The
[Foo(1)] would create nested lists which are not the desired result– marthin23
Jan 18 at 21:51
@jonrsharpe I added a better example. The
[Foo(1)] would create nested lists which are not the desired result– marthin23
Jan 18 at 21:51
1
1
Why are you trying to pass both
Foo instances and sequences of Foo instances to concatenate? That seems like a bad idea.– user2357112
Jan 19 at 6:20
Why are you trying to pass both
Foo instances and sequences of Foo instances to concatenate? That seems like a bad idea.– user2357112
Jan 19 at 6:20
add a comment |
1 Answer
1
active
oldest
votes
You could just check for lists and use .extend to concatenate, or .append to add non-list items:
class Foo:
def __init__(self, value):
self.value = value
def __repr__(self):
return f'Foo({self.value})'
def concat(*args):
result =
for arg in args:
if isinstance(arg,list):
result.extend(arg)
else:
result.append(arg)
return result
a = concat(Foo(1),Foo(2),Foo(3))
print(a)
b = concat(Foo(6),Foo(7))
print(b)
c = concat(a,Foo(4),Foo(5),b)
print(c)
Output:
[Foo(1), Foo(2), Foo(3)]
[Foo(6), Foo(7)]
[Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]
Brilliant solution! Thanks a lot
– marthin23
Jan 20 at 19:57
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%2f54261640%2fpython-list-function-on-non-iterable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could just check for lists and use .extend to concatenate, or .append to add non-list items:
class Foo:
def __init__(self, value):
self.value = value
def __repr__(self):
return f'Foo({self.value})'
def concat(*args):
result =
for arg in args:
if isinstance(arg,list):
result.extend(arg)
else:
result.append(arg)
return result
a = concat(Foo(1),Foo(2),Foo(3))
print(a)
b = concat(Foo(6),Foo(7))
print(b)
c = concat(a,Foo(4),Foo(5),b)
print(c)
Output:
[Foo(1), Foo(2), Foo(3)]
[Foo(6), Foo(7)]
[Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]
Brilliant solution! Thanks a lot
– marthin23
Jan 20 at 19:57
add a comment |
You could just check for lists and use .extend to concatenate, or .append to add non-list items:
class Foo:
def __init__(self, value):
self.value = value
def __repr__(self):
return f'Foo({self.value})'
def concat(*args):
result =
for arg in args:
if isinstance(arg,list):
result.extend(arg)
else:
result.append(arg)
return result
a = concat(Foo(1),Foo(2),Foo(3))
print(a)
b = concat(Foo(6),Foo(7))
print(b)
c = concat(a,Foo(4),Foo(5),b)
print(c)
Output:
[Foo(1), Foo(2), Foo(3)]
[Foo(6), Foo(7)]
[Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]
Brilliant solution! Thanks a lot
– marthin23
Jan 20 at 19:57
add a comment |
You could just check for lists and use .extend to concatenate, or .append to add non-list items:
class Foo:
def __init__(self, value):
self.value = value
def __repr__(self):
return f'Foo({self.value})'
def concat(*args):
result =
for arg in args:
if isinstance(arg,list):
result.extend(arg)
else:
result.append(arg)
return result
a = concat(Foo(1),Foo(2),Foo(3))
print(a)
b = concat(Foo(6),Foo(7))
print(b)
c = concat(a,Foo(4),Foo(5),b)
print(c)
Output:
[Foo(1), Foo(2), Foo(3)]
[Foo(6), Foo(7)]
[Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]
You could just check for lists and use .extend to concatenate, or .append to add non-list items:
class Foo:
def __init__(self, value):
self.value = value
def __repr__(self):
return f'Foo({self.value})'
def concat(*args):
result =
for arg in args:
if isinstance(arg,list):
result.extend(arg)
else:
result.append(arg)
return result
a = concat(Foo(1),Foo(2),Foo(3))
print(a)
b = concat(Foo(6),Foo(7))
print(b)
c = concat(a,Foo(4),Foo(5),b)
print(c)
Output:
[Foo(1), Foo(2), Foo(3)]
[Foo(6), Foo(7)]
[Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]
answered Jan 19 at 6:53
Mark TolonenMark Tolonen
92.7k12112176
92.7k12112176
Brilliant solution! Thanks a lot
– marthin23
Jan 20 at 19:57
add a comment |
Brilliant solution! Thanks a lot
– marthin23
Jan 20 at 19:57
Brilliant solution! Thanks a lot
– marthin23
Jan 20 at 19:57
Brilliant solution! Thanks a lot
– marthin23
Jan 20 at 19:57
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%2f54261640%2fpython-list-function-on-non-iterable%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
3
list((Foo(1),))? Or just[Foo(1)]?– jonrsharpe
Jan 18 at 21:30
It sounds like you just need an explicit base case for when you have a single item on the left hand side (or no items returning an empty list, depending what you're doing).
– Patrick Haugh
Jan 18 at 21:32
@jonrsharpe I added a better example. The
[Foo(1)]would create nested lists which are not the desired result– marthin23
Jan 18 at 21:51
1
Why are you trying to pass both
Fooinstances and sequences ofFooinstances toconcatenate? That seems like a bad idea.– user2357112
Jan 19 at 6:20