Hello, @TL_89,
Your solution for the stated problem statement is almost correct. However, you are only appending one pair inside the ret
list. For instance, if i[1] in dict and dict[i[1]] == i[0]
, it only appends one pair and not the corresponding symmetric pair, i.e., it only appends [3, 4]
and misses out the [4, 3]
pair and hence throwing an error. Adding this line inside the final if
statement resolves the error:
ret.append([i[1], i[0]])
Final solution then becomes:
def find_symmetric(my_list):
ret = []
dict = {}
for i in my_list:
if len(i)==2:
dict[i[0]] = i[1]
if i[1] in dict and dict[i[1]] == i[0]:
ret.append(i)
ret.append([i[1], i[0]])
return ret
Hope this answers your query. Feel free to ask any questions if you still have any confusion.