4

I am trying to modify a list of two lists. For each of the two inside lists, I perform some operation and 'split' them into new lists.
Here is a simple example of what I'm trying to do:

[['a', 'b'], ['c', 'd']]  -->  [['a'], ['b'], ['c', 'd']]

Currently my algorithm passes ['a', 'b'] to a function that determines whether or not it should be split into [['a'], ['b']] (e.g. based on their correlations). The function returns [['a'], ['b']] which tells me that ['a', 'b'] should be split, or returns ['a', 'b'] (the original list) which indicates that it should not be split.

Currently I have something like this:

blist = [['a', 'b'], ['c', 'd']]   #big list
slist =  [['a'], ['b']]            #small list returned by function

nlist = [items for i in xrange(len(blist)) for items in (slist if i==0 else blist[i])]

This produces [['a'], ['b'], 'c', 'd'] as opposed to the desired output [['a'], ['b'], ['c', 'd']] which does not alter the second list in the original blist. I understand why this is happening--my second loop is also applied to blist[1] in this case, but I am not sure how to fix it as I do not understand list comprehension completely.

A 'pythonic' solution is preferred. Any feedback would be appreciated, thank you!

EDIT: Like the title suggests, I am trying to 'replace' ['a', 'b'] with ['a'], ['b']. So I would like the 'position' to be the same, having ['a'], ['b'] appear in the original list before ['c', 'd']

RESULTS Thank you Christian, Paul and schwobaseggl for your solutions! They all work :)

3
  • You are contradicting yourself. Are you passing items to a function or are you using a nested list comprehension? Anyway, if you don't understand list comprehensions, why don't you use simple for-loops until you do? I would.
    – user3850
    Feb 16, 2017 at 15:24
  • Sorry, I meant that the function is used to help determine whether or not the list is splitted, the nested list comprehension performs the change/split Feb 16, 2017 at 15:26
  • @hop I believe I understand simple for-loops. I would like to be more familiar with list comprehensions and have a better understanding. Just trying to get some practice. Also by 'I would' are you suggesting that only using simple for loops helps with understanding list comprehensions? I understand that they have similarities, but like in my example, the list comprehension seems to be a lot more compact with the statements in different orders. Could you elaborate more on the relationship between the two? Feb 16, 2017 at 15:40

4 Answers 4

2

Try

...  else [blist[i]])]

to create a list of lists.

0
1

You can use slice assignment:

>> l1 = [[1, 2], [3, 4]]
>>> l2 = [[1], [2]]
>>> l1[0:1] = l2
>>> l1
[[1], [2], [3, 4]]

This changes l1, so if you want to keep it make a copy before.

Another way that doesn't change l1 is addition:

>> l1 = [[1, 2], [3, 4]]
>>> l3 = l2 + l1[1:]
>>> l3
[[1], [2], [3, 4]]
11
  • 1
    no, that should put the elements of l2 which are 1 and 2 in place of the element currently sitting at position zero of l1 which is [1, 2], in other words it loses the brackets around 1 and 2. Feb 16, 2017 at 15:35
  • 1
    It makes a so-called slice copy, it should be more or less equivalent to .copy(). Note that this is different in numpy where slicing doesn't create a copy but a view. Feb 16, 2017 at 15:44
  • 1
    You are not changing l1 or rather the object bound to the name l1 you are binding a new object (the product of your list comprehension) to l1. A list comprehension does produce a new list, so it needs to alllocate space for that. I don't really now, but I would guess list comprehensions are pretty efficient, that said if you are only changing one bit and copying all the rest over, then slice assignment migh be the better choice. Actually, the slice copy I'm using on the right hand side in the answer is not necessary. Sorry, I'll fix that. Feb 16, 2017 at 16:01
  • 1
    I think the basic logic is l1[0:1] gives a copy of this part of l1 as a list. So it should also work the other way round. if you assign to l1[0:1] it should accept a list, makee a copy and place the list at exactly the same spot. Note that this can change the length of the list. Feb 16, 2017 at 16:08
  • 1
    I think if you use the syntax for assignment, no copy is made of this bit. Why should there be? The bit is not needed. However, since you are potentially changing the length of the list some comparatively expensive reorganisation may be necessary. I don't know the internals well enough to be more specific here. But these considerations really only are worthwhile if you are handling large lists maybe 100,000 elements or more or making lots of lists or looping over them lots of times. For garden variety lists you won't even notice. Feb 16, 2017 at 16:23
1

You could alter your split function to return structurally adequate lists. Then you can use a comprehension:

def split_or_not(l):
  if condition: # split
    return [l[:1], l[1:]]
  return [l]  # wrap in extra list

# using map
nlist = [x for sub_l in map(split_or_not, blist) for x in sub_l]
# or nested comprehension
nlist = [x for sub_l in (split_or_not(l) for l in blist) for x in sub_l]
8
  • That is a very good point! I upvoted but it doesn't show since I have less than 15 reputation haha. I will modify the return object structures. In addition, what happens when [l] is returned instead of l? Does it require any additional time to put l into a list? Feb 16, 2017 at 15:59
  • 1
    The new list only holds a reference to the list, the overhead should be negligible! Feb 16, 2017 at 16:16
  • If you use a list.extend() and a normal for-loop, you can reduce the complexity of the code considerably. Nested list comprehensions should be avoided (speaking of "pythonic").
    – user3850
    Feb 16, 2017 at 16:28
  • @hop Do you have a source for that last statement. To my knowledge, nested comprehensions are performant in comparison to other methods, see e.g. http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python Feb 16, 2017 at 16:56
  • 1
    @hop While I (mostly) agree with readability > performance, I don't think any of the examples in this answer suffer from poor readability. I will admit that being used to it has sth. to do with it, but still more a matter of taste than Pythonicity ;) Feb 16, 2017 at 17:27
0

Assuming you have the mentioned funtion that decides whether to split an item:

def munch(item):
    if item[0] == 'a': # split
        return [[item[0]], [item[1]]]
    return [item] # don't split

You can use it in s simple for-loop.

nlist = []
for item in blist:
    nlist.extend(munch(item))

"Pythonic" is whatever is easy to read and understand. Don't use list comprehensions just because you can.

2
  • could you provide a brief explanation of what munch does here? An example would be really helpful. Thank you! Feb 16, 2017 at 15:59
  • @AsheKetchum: You can easily put the code into the Python command line and try it for yourself.
    – user3850
    Feb 16, 2017 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.