3

I want to iterate through a list that contains a mix of lists and non-list elements. Here's an example:

a = [[1, 2, 3, 4, 5], 8, 9, 0, [1, 2, 3, 4, 5]] 

I know how to iterate through a mandatory list of lists, but in this case I don't know how to do it. My objective is to compare one value of the nested list with another value in the list. For example in this case: [1, 2, 3, 4, 5] and 8

4
  • 4
    Please give an example of what you want with both input and output. Commented May 18, 2012 at 20:19
  • @robert i want to compare one value outside the inner list, with the numbers within the inner list, and if they are equal, delete the value in the inner list Commented May 18, 2012 at 20:22
  • 2
    I'm still not sure exactly what you want. Please show an example. Commented May 18, 2012 at 20:23
  • @Andfoy check out my solution. Commented May 18, 2012 at 21:05

4 Answers 4

4

Is this what you want:

thelist = [[1, 2, 3, 4, 5], 5, 6, 7, 8, 10, [9, 0, 1, 8]]
# Remove the 5 from the first inner list because it was found outside.
# Remove the 8 from the other inner list, because it was found outside.
expected_output =[[1, 2, 3, 4], 5, 6, 7, 8, 10, [9, 0, 1]]

Here's a way to do it:

thelist = [[1, 2, 3, 4, 5], 5, 6, 7, 8, [9, 0, 1, 8]]

expected_output =[[1, 2, 3, 4], 5, 6, 7, 8, [9, 0, 1]]

removal_items = []

for item in thelist:
    if not isinstance(item, list):
        removal_items.append(item)

for item in thelist:
    if isinstance(item, list):
        for remove in removal_items:
            if remove in item:
                item.remove(remove)

print thelist

assert thelist == expected_output
Sign up to request clarification or add additional context in comments.

2 Comments

So i must separate the lists and the outside values, compare, delete and create again the list?
@Andfoy, you can use filter(lambda elem: type(elem) is not list, a), which is a built-in function that will remove any list element from your main list. As for create again the list, you don't have to. The code from @jgritty modifies your original list directly.
3

A slightly different version than the answer from jgritty. The differences:

  • we use the filter() built-in to extract the int elements from your list
  • we keep the integers in a set instead of a list
  • we iterate over a copy of a, so that we can safely remove elements from a itself at the same time
  • use list comprehension to remove members of nested lists that are already in the main list

    a = [[1, 2, 3, 4, 5], 5, 6, 7, 8, [9, 0, 1, 8]]
    print a
    
    numbers = set(filter(lambda elem: type(elem) is not list, a))
    
    for elem in a:
        if type(elem) is list:
            elem[:] = [number for number in elem if number not in numbers]
    
    print a
    

4 Comments

Is there a reason you do for elem in a[:]: instead of for elem in a:?
Good question. a[:] is an idiomatic way of getting a copy of a list in Python. It is not a good idea to modify a list while iterating over it, as explained in the online doc
It's really only the elem list that needs to be copied I think. It doesn't seem that you're modifying a, only its elements. Also, I don't iterate over the inner lists for exactly that reason.
@jgritty good point, there is indeed no need to get a copy of the outer list. I modified my example accordingly. Also, agreed that your version works.
2
a = [[1, 2, 3, 4, 5], 8, 9, 0, [1, 2, 3, 4, 5]]
for x in a:
    if type(x) is list:
        for y in x:
            print y
    else:
        print x

or use

isinstance(x, list)

2 Comments

isinstance is usually preferable than type
Definitely use isinstance, have that as the only way and remove your check of type.
0

You can check if the object of the iteration is a ListType (http://docs.python.org/library/types.html) and iterate it further.

I cant remember now the exact command but there is something like type(x) that you can use to get the object's type.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.