Reputation: 655
I have a problem where I am given a list of lists having strings. I have to reverse not only the strings but also output a list of lists in reverse way
Example :
input_list = [["CAT", "BAT", "SAT"], ["MAN","CHAN", "LAN"], ["CAL", "NJ","NYC"]]
EXPECTED OUTPUT :
[["CYN", "JN", "LAC"], ["NAL", "NAHC", "NAM"], ["TAS", "TAB", "TAC"]]
Code I am trying :
doc_file = [["CAT", "BAT", "SAT"],
["MAN","CHAN", "LAN"],
["CAL", "NJ","NYC"]]
temp_list = []
temp_list2 = []
#print("reversing First Line:")
rev_main_list = list(reversed(doc_file))
#print(rev_main_list)
for i in range(len(rev_main_list)):
for j in range(len(rev_main_list[i])):
# print("value of i:", i)
# print("value of j:", j)
temp_list.append((rev_main_list[i][j][::-1]))
#print("outer loop i:", i)
temp_list2.append(temp_list[i])
rev_main_list =temp_list2
print(rev_main_list)
Your help is much appreciated
Upvotes: 1
Views: 34
Reputation: 623
for small_list in big_list:
for x in small_list:
x = x[::-1]
small_list.reverse()
big_list.reverse()
Bit of a clunky fix, but it should work for you.
Upvotes: 0
Reputation: 113950
def reverse_it(a):
if isinstance(a,(list,tuple)):
return [reverse_it(x) for x in a[::-1]]
return a[::-1]
Upvotes: 1