Locating sublists of one list in another list in Python
Question:
I have two lists G3, G333
. I want to locate the sublists of G333
in G3
and print the indices i
. For example, [0, 4, 5, 9]
occurs at i=0
in G3
, [10, 14, 15, 19]
occurs at i=5
in G3
. But I am getting an error. I present the expected output.
G3=[[0, 4, 5, 9], [1, 5, 6, 10], [2, 6, 7, 11], [3, 7, 8, 12], [9, 13, 14, 18], [10, 14, 15, 19]]
G333=[[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
G3s = set(map(tuple, G3))
G333s = set(map(tuple, G333))
for i in range(0,len(G3s)):
if (G3s[i] & G333s[i]):
print(i)
The error is
in <module>
if (G3s[i] & G333s[i]):
TypeError: 'set' object is not subscriptable
The expected output is
i=[0,1,5]
Answers:
You can directly use list.index
to get the index.
for l in G333:
print(G3.index(l))
Or, with a list comprehension:
i = [G3.index(l) for l in G333] # [0, 1, 5]
Here’s another way to do it:
G3 = [
[0, 4, 5, 9],
[1, 5, 6, 10],
[2, 6, 7, 11],
[3, 7, 8, 12],
[9, 13, 14, 18],
[10, 14, 15, 19],
]
G333 = [[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
for idx, sublist in enumerate(G3):
if sublist in G333:
print(idx)
The above solution uses enumerate
to iterate through G3
members, while also retrieving each element from G3
to compare with the sub-lists from
G333
.
If you want to use the same approach as your current solution, you could
use something like:
G3 = [
[0, 4, 5, 9],
[1, 5, 6, 10],
[2, 6, 7, 11],
[3, 7, 8, 12],
[9, 13, 14, 18],
[10, 14, 15, 19],
]
G333 = [[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
G3s = set(map(tuple, G3))
G333s = set(map(tuple, G333))
for element in G333s & G3s:
print(G3.index(list(element)))
However, I personally find the first option to be easier to understand and use.
Side-note: when using for...loops
, avoid using i
as name for the elements of your iterator. Rather, try names that better explain what each element represents. For iterating through indexes, for example, I prefer using the name idx
(it’s a common shorthand for index).
You can use index function and if any item is missing that can be handled like below:
G3=[[0, 4, 5, 9], [1, 5, 6, 10], [2, 6, 7, 11], [3, 7, 8, 12], [9, 13, 14, 18], [10, 14, 15, 19]]
G333=[[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
i = []
for item in G333:
try:
i.append(G3.index(item))
except ValueError:
pass
print(i)
I have two lists G3, G333
. I want to locate the sublists of G333
in G3
and print the indices i
. For example, [0, 4, 5, 9]
occurs at i=0
in G3
, [10, 14, 15, 19]
occurs at i=5
in G3
. But I am getting an error. I present the expected output.
G3=[[0, 4, 5, 9], [1, 5, 6, 10], [2, 6, 7, 11], [3, 7, 8, 12], [9, 13, 14, 18], [10, 14, 15, 19]]
G333=[[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
G3s = set(map(tuple, G3))
G333s = set(map(tuple, G333))
for i in range(0,len(G3s)):
if (G3s[i] & G333s[i]):
print(i)
The error is
in <module>
if (G3s[i] & G333s[i]):
TypeError: 'set' object is not subscriptable
The expected output is
i=[0,1,5]
You can directly use list.index
to get the index.
for l in G333:
print(G3.index(l))
Or, with a list comprehension:
i = [G3.index(l) for l in G333] # [0, 1, 5]
Here’s another way to do it:
G3 = [
[0, 4, 5, 9],
[1, 5, 6, 10],
[2, 6, 7, 11],
[3, 7, 8, 12],
[9, 13, 14, 18],
[10, 14, 15, 19],
]
G333 = [[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
for idx, sublist in enumerate(G3):
if sublist in G333:
print(idx)
The above solution uses enumerate
to iterate through G3
members, while also retrieving each element from G3
to compare with the sub-lists from
G333
.
If you want to use the same approach as your current solution, you could
use something like:
G3 = [
[0, 4, 5, 9],
[1, 5, 6, 10],
[2, 6, 7, 11],
[3, 7, 8, 12],
[9, 13, 14, 18],
[10, 14, 15, 19],
]
G333 = [[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
G3s = set(map(tuple, G3))
G333s = set(map(tuple, G333))
for element in G333s & G3s:
print(G3.index(list(element)))
However, I personally find the first option to be easier to understand and use.
Side-note: when using for...loops
, avoid using i
as name for the elements of your iterator. Rather, try names that better explain what each element represents. For iterating through indexes, for example, I prefer using the name idx
(it’s a common shorthand for index).
You can use index function and if any item is missing that can be handled like below:
G3=[[0, 4, 5, 9], [1, 5, 6, 10], [2, 6, 7, 11], [3, 7, 8, 12], [9, 13, 14, 18], [10, 14, 15, 19]]
G333=[[0, 4, 5, 9], [1, 5, 6, 10], [10, 14, 15, 19]]
i = []
for item in G333:
try:
i.append(G3.index(item))
except ValueError:
pass
print(i)