I have written a code where I am getting a 3 dimensional list, I am trying to convert it to 1 dimensional.
Asked
Active
Viewed 1,446 times
-3
-
1`np.ravel(input_list).tolist()`? – Divakar Nov 10 '17 at 09:52
-
@Divakar there must be a dupe of this – EdChum Nov 10 '17 at 09:53
-
You have a list of list of lists or a ndarray with three axes? Better show the code to make it clear. – Ignacio Vergara Kausel Nov 10 '17 at 09:54
-
1Flattening a python list: https://stackoverflow.com/q/2158395/812912 flattening a numpy ndarray: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.flatten.html – Ivaylo Strandjev Nov 10 '17 at 09:55
1 Answers
1
Here are two simple solutions to the problem:
originl_list = [[[1,2,3], [1,2,3]], [[1,2,3], [1,2,3]]]
final_list = []
def method1():
for i in lst1:
for j in i:
for k in j:
final_list.append(k)
print(final_list)
def method2():
[final_list.append(k) for i in originl_list for k in i for j in k]
print(final_list)

S. Allen
- 171
- 1
- 10