My Data This picture shows my data. See this: [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110,....]
, I want this data to be like : [1,0,32,64,117,115......]
. I want data out of the list and become a member of the array. How can I do this easily and quickly? I have tried a long method but there are many errors. The method I have tried: Take data out of the list one by one and append in the particular row then move to the next data row and do the same thing there for the list.
Asked
Active
Viewed 778 times
0

Buddy Bob
- 5,829
- 1
- 13
- 44

umar anjum
- 1
- 1
-
Can you add what've tried ? – Chiheb Nexus May 10 '21 at 22:10
-
Does this answer your question? [How to make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) – Woodford May 10 '21 at 22:15
4 Answers
0
you can try something like this :
`for i in range(len(data)) :
data[i][2].insert(0,data[i][1])
data[i][2].insert(0,data[i][0])
data[i].pop(1)
data[i].pop(0)`

melike
- 1
- 1
0
You could do something like below.
We iterate through the given list, checking each type. If an element is a type list
we will iterate through that and append each of those elements to a list called new
. If an element we have iterated through in the given list is a type != list
then we can directly append the element to new
.
x = [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110])]
new = []
for num in x:
if type(num) == list:
for num2 in num:new.append(num2)
else:new.append(num)

Buddy Bob
- 5,829
- 1
- 13
- 44
0
You can flatten this jagged nested array with numpy.hstack() as follows:
import numpy as np
original = [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110])]
f = np.hstack(np.array(original, dtype=object)).tolist()
print(f)
#[1, 0, 32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]

pakpe
- 5,391
- 2
- 8
- 23
-1
Let first consider this example
# lets take this example value for data
data = [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110])]
print("data before modification:", data)
print()
extract_items = data.pop(2) # Since in the picture, the inner list is always the third element we can use the index 2
for item in extract_items: # loop through the inner list and append items to outer
data.append(item)
print("extract_items", extract_items)
print("data", data)
**output for example case: **
data before modification: [1, 0, [32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]]
extract_items: [32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]
data: [1, 0, 32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]
Which is what we would like.
Thus, the Final Solution:
for element in data:
extract_items = element.pop(2)
for item in extract_items:
element.append(item)

Muhd Mairaj
- 557
- 4
- 13
-
1Looks like you are using pop. I don't recommend this method mainly because sometimes you might not know where your sublist is located. Look at my example for a non hardcoded example. – Buddy Bob May 10 '21 at 23:25
-
Fair enough. Looking at his data though i assumed it was known, because every sublist had the same pattern, would probably do something similar to your method otherwise with the help of type() method – Muhd Mairaj May 11 '21 at 03:50