tags:

views:

143

answers:

6
+1  Q: 

Splitting a list

Hello,

I've scoured various resources and can't figure out how to do a rather simple operation.

Right now, I have a list as follows:

li = [['a=b'],['c=d']]

I want to transform this into:

li = [['a','b'],['c','d']]

As I understand it, split("=") only applies to string types. Is there an equivalent method for lists?

Pardon the simplicity of my question...

-Dan

A: 

Warning - its been a while since I did any python, but your issue is more general.

You are correct in that split applies to strings.

What you need to do is split the VALUE contained in your list not the list itself.

So you would do something like

newValue = split('=', li[0][0]) 
li[0] = newValue
Freiheit
+3  A: 

You can use map():

>>> li = [['a=b'],['c=d']]
>>> map(lambda x: x[0].split('='), li)
[['a', 'b'], ['c', 'd']]

This traverses the list li and applies the lambda function to every element. As every element of the list is again a list with one element, x[0] takes this element, which is a string, splits it and returns a new list with both values.

Felix Kling
+10  A: 

You want this:

[x[0].split('=') for x in li]
# prints [['a', 'b'], ['c', 'd']]

To grab a question from a comment further down the post, the reason split works for x[0] is that x represents the inner list. That's accomplished by the for x in li. Also, I fixed mine to read for x in li and not for x in test as I had assigned your examples to a variable called 'test' on my system.

g.d.d.c
This works only if each inner list has only one element.
chryss
The same is true for the answers that suggest map as well, the key being that they use x[0] to get at the string in the inner list. If the OP has more complicated examples of his nested lists the solutions can be revisited, but this answers the question as asked.
g.d.d.c
This will work for no matter how many elements: `[x[i].split('=') for x in li for i in range(len(x))]`
Zonda333
A: 

Is this what you are looking for ? map(lambda y:y.split('='),map(lambda x:x[0], li))

Gangadhar
A: 

Presuming each sublist consists of individual strings of the form a=b:

>>> [el[i].split('=') for el in li for i in range(len(el))]
[['a', 'b'], ['c', 'd']]

(Indeed, what you're splitting is the inner string a=b. So the split() string method works fine.)

EDIT: A much more elegant way of doing this double list comprehension is:

>>> [a.split('=') for el in li for a in el]
[['a', 'b'], ['c', 'd']]

There have been a number of good suggestions made, so the OP should be able to learn a good amount of Python for it. Important to remember is that what is being split is li[i][j], ie an item of the list that is an item of the list li.

chryss
`range(len(el))` is wrong! The following is simpler: `[val.split('=') for el in li for val in el]`
PreludeAndFugue
Uh, I already added this long before you commented. Also, could you elaborate on "wrong"?
chryss
As you demonstrated in your edit - there is a simpler way to achieve the same result. Therefore, I would say the first version is 'wrong'.
PreludeAndFugue
Well, that's an interesting definition of wrong. Mostly, though, I'm not going to remove the initial option, given it returns the right result, because I want this post to remain readable in its history. It would be nice if someone edited the "winning" response to integrate what others have contributed.
chryss
A: 

You can do it with this:

[k[0].split("=") for k in li]
txwikinger
This works only if each inner list has only one element. My solution presumes that if it has several elements, they're all of the indicated form.
chryss
@chryss: I would not assume this. If so the OP should clarify.
Felix Kling
Why does split work on k[0] and not li itself? Isn't k a list here as well?
Dan
@Dan: `k` is a list, but `k[0]` is the first element of that list. And `k[0] == li[i][0]` where `i` is either `0` or `1` in you example. It is just a nested list, nothing magical ;)
Felix Kling
li is a list of lists with one element in it. If you would li being ['a=b','c=d'], then you would use k instead of k[0]
txwikinger