So the Goal is: given a Nested list that contains 3 elements in each element in the nest, i.e. L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] how can I take a flat list, i.e. L2=[1,2,3,4,11] where if the elements within the nested list, i.e. L1[0], L1[1], etc. intersect with any single element in L2, the element within the nested list is returned into a different list, L3

OK, here's what I have:
L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2=[1,2,3,4,11]
L3=[]
u=0
s=0
while s<=len(L2):
if L2[s] == L1[0][0]:
L1.pop(0)
if L2[s] == L1[0][1]:
L1.pop(0)
if L2[s] == L1[0][2]:
L1.pop(0)
else:
listy=L2[s+1]
while u<=JDAWGlen:
if listy == L1[0]:
L1.pop(u)
if listy == L1[1]:
L1.pop(u)
if listy == L1[2]:
L1.pop(u)
else: break
u+=1
s+=1


But this is where I freeze. How can I get the "popped" lists into a list L3? L3 should be L3=[[1,2,3],[4,5,6],[10,11,12]]

Maybe there is a much faster and better way to go about this- maybe by using a "for" loop. Or maybe there is a much much better way with a dictionary, but I have no idea how to use it for this. so step by step instructions would be helpful.

Recommended Answers

All 8 Replies

Using for() loops is easier. This is somewhat of a brute force method but works fine for smaller lists.

L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2=[1,2,3,4,11]
L3=[]

for number in L2:
    for a_list in L1:
        if (number in a_list) and (a_list not in L3):
            L3.append(a_list)
print L3 
##
##   You can also delete from the list of lists as you add it
##   to L3 so you don't search through it again, but deleting 
##   can cause problems in some cases
print "-" * 50
L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L1_copy = L1[:]
L2=[1,2,3,4,11]
L3=[]
for number in L2:
    for a_list in L1:
        if (number in a_list) and (a_list not in L3):
            L3.append(a_list)
            L1.remove(a_list)
            print "-----> L1 =", L1
print L3
commented: Nice one. Beat me to it, and more cleverly too! +1

Here's how I would approach this:

(1) for each entry in L2 find all the matching sub-lists from L1. You can use a list comprehension or a for loop for this.
(2) Add all the sub-lists that matched into a single list
(3) Reduce the list containing all the sub-lists to just the unique entries.

To get you started here's the code for the first step using a for loop:

L1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2 = [1,2,3,4,11]

for item in L2:
    # entry will contain all the sub lists from L1 that contain the item
    entry = []
    for sub_list in L1:
        if item in sub_list:
            entry.append(sub_list)

One of the solutions is pretty straight forward, with easy logic:

list1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
list2 = [1,2,3,4,11]

list3 = []
# q1 is each sublist in list1
for q1 in list1:
    # q2 is each item in list2
    for q2 in list2:
        # do not append if sublist is already in list3
        if q2 in q1 and q1 not in list3:
            list3.append(q1)

print(list3)

"""my output -->
[[1, 2, 3], [4, 5, 6], [10, 11, 12]]
"""

There may be better solutions.
Sorry, woooee already had this solution!!

>>> L1
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>> L2
[1, 2, 3, 4, 11]
>>> L3 = []
>>> for x in L1:
	for y in L2:
		if y in x and x not in L3:
			L3.append(x)

			
>>> L3
[[1, 2, 3], [4, 5, 6], [10, 11, 12]]

Is anyone else besides me thinking Python sets might be a good tool to use to solve this?

Edit: Actually this probably won't work. According to Python documentation on sets, sets are containers for unique objects.

Excellent! Thanks everyone.

A shorter method

L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2=[1,2,3,4,11]

set2 = set(L2)
L3 = [x for x in L1 if set2.intersection(x)]
print(L3)

Thought of another way to do this:

>>> L1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>> L2 = [1,2,3,4,11]
>>> [y for y in L1 if [x for x in L2 if x in y] != []]
[[1, 2, 3], [4, 5, 6], [10, 11, 12]]
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.