tags:

views:

83

answers:

5

Maybe the question is a bit vague, but what I mean is this code:

'livestream' : [cow.legnames for cow in listofcows]

Now the problem is cow.legnames is also a list so I will get a list in a list when I try to return it with Json. How should I make it to return a single list.

This is the json that would be returned.

'livestream' : [['blue leg', 'red leg']]

I hope the code explains what my question is.

+1  A: 

From my understanding, you have something like this:

class Cow(object):
    def __init__(self, legnames):
        self.legnames = legnames

listofcows = [Cow(['Red Leg', 'Blue Leg']), 
              Cow(['Green Leg', 'Red Leg'])]

It's easiest extend a temporary list, like this:

legs = []

# As @delnan noted, its generally a bad idea to use a list
# comprehension to modify something else, so use a for loop
# instead.
for cow in listofcows:
    legs.extend(cow.legnames)


# Now use the temporary legs list...
print {'livestream':legs}
Joe Kington
List comprehensions with side effects are bad enough, but a list comprehension solely for side effects is simply unpythonic and horrible. Use a for-loop.
delnan
@delnan - I agree, but I was trying to mirror what the OP was doing to make things clearer to him.
Joe Kington
Edit and state that this is a bad idea and just an example, and I'll undownvote :)
delnan
Yea I will go with a 2 line for loop. Thought maybe there was a shortway by removing the list [] symbols but that didn't work either.
Sam S
`for cow in listofcows: legs.extend(cow.legnames)` does work without linebreak after the colon.
delnan
Beware, StackOverflow seems to have changed its system rules recently and you can't undownvote after a certain amount of time.
uosɐſ
@uosɐſ You can always change your downvote if the entry has since been edited.
cobbal
And just in time, there comes the edit - undownvoted; it was a pleasure to deal with you, sir `:p`
delnan
Yea. I just made it the same way as you with the for loop. Seems it's better in this case.
Sam S
+2  A: 

The name listofcows implies there may be, possibly in a distant future, several cows. Flattening a list of list with more than one item would be simply wrong.

But if the name is misleading (and why a one-lement list anyway, for that matter?), you have several options to flatten it.

Nested list comprehension: [legname for cow in listofcows cow.legnames for legname in cow.legnames]

Getting the first item: [your list comprehension][0]

And very likely some useful thingy from the standard library I don't remember right now.

delnan
@delnan - As to the "useful thingy", I think you're thinking of `sum(nested_list, [])`
Joe Kington
A: 

Is this what you're looking for?

'livestream' : [cow.legnames[0] for cow in listofcows]
Adam Nelson
No It seems the answer by Joe is the most suited as it's not so good to use the comprehension list here.
Sam S
A: 

you can try reduce,
'livestream' : reduce(lambda a,b:a+b,[cow.legs for cow in listofcows])

if you need unique legs use set

'livestream' :list(set(reduce(lambda a,b:a+b,[cow.legs for cow in listofcows])))

Tumbleweed
I have never used lambda. It seems not s onice to readability to me, but maybe if you understand it, it's the best way.
Sam S
+4  A: 

In addition to shahjapan's reduce you can use this syntax to flatten list.

[legname for cow in listofcows for legname in cow.legnames]
Odomontois
+1 beat me too it.
aaronasterling
I didn't have time to test this answer out yet. But I'm guessing this is the right answer. Will accept your answer as soon as I try it out, but im quite busy right now. Thanks for your answer though. Appreciate it.
Sam S
Works perfect. This was the way I wanted to do it. Didn't know I would be able to do 2 for loops in one line ;)
Sam S