Reputation: 57
Good evening,
I am trying to create a singular list from multiple different lists, but when i merge them it just gives me [listA], [ListB]
instead of ListA, ListB
i dont know how to explain so ill just show what i have tried and show my expectation.
here is my original code.
N, B, E = "N", "B", "E"
data = [N, N, N, N, N, N, N,
N, N, N, N, N, N, N,
N, N, N, B, E, N, E,
N, N, N, N, B, E, N,
B, B, B, B, B, B, B,
N, E, N, B, N, N, N]
and when i print data
i get the result
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'B', 'E', 'N', 'E', 'N', 'N', 'N', 'N', 'B', 'E', 'N', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'N', 'E', 'N', 'B', 'N', 'N', 'N']
thats what i want to happen, but i quickly realised i would not be able to edit it easily in this format. so i tried making 6 different lists and converting them into one.
N, B, E = "N", "B", "E"
Data6 = [N, N, N, N, N, N, N]
Data5 = [N, N, N, N, N, N, N]
Data4 = [N, N, N, B, E, N, E]
Data3 = [N, N, N, N, B, E, N]
Data2 = [B, B, B, B, B, B, B]
Data1 = [N, E, N, B, N, N, N]
So that is my base, i tried 2 ways to merge them but both of them proved unsuccessful, after googling around for a while i still got nothing
my 2 attempts where:
data = [Data6,
Data5,
Data4,
Data3,
Data2,
Data1]
print(data)
data = []
data.append(Data6)
data.append(Data5)
data.append(Data4)
data.append(Data3)
data.append(Data2)
data.append(Data1)
print(data)
im expecting the result that i get from the pre created list but instead i get
[['N', 'N', 'N', 'N', 'N', 'N', 'N'], ['N', 'N', 'N', 'N', 'N', 'N', 'N'], ['N', 'N', 'N', 'B', 'E', 'N', 'E'], ['N', 'N', 'N', 'N', 'B', 'E', 'N'], ['B', 'B', 'B', 'B', 'B', 'B', 'B'], ['N', 'E', 'N', 'B', 'N', 'N', 'N']]
If anyone knows how to help or what i am doing wrong, it would be greatly appreciated
Upvotes: 1
Views: 780
Reputation: 83
This happens because you are inserting lists inside a list. This operation simply puts a pointer to the list A into list B. So you end up having a list B which contains pointers to another lists. You can use concatenation (+) or array.extend() as Rakesh suggests.
Upvotes: 1
Reputation: 11
If I understand correctly, you just need to concatenate the lists :
DATA = Data6 + Data5 + Data4 + Data3 + Data2 + Data1
Upvotes: 1
Reputation: 118
Just run a for loop over the contents of data1,data2 and Soo on , and just push the elements
Since you are pushing all the list , therefore they ouutput is as list of list
New_list = [*data1, *data2 ] and Soo on , new method in python3.6
Upvotes: 0
Reputation: 1493
To make it efficient, you can use for
loop to concatenate the list. No matter how many lists you want to concatenate, you don't need to write one by one.
N, B, E = "N", "B", "E"
Data6 = [N, N, N, N, N, N, N]
Data5 = [N, N, N, N, N, N, N]
Data4 = [N, N, N, B, E, N, E]
Data3 = [N, N, N, N, B, E, N]
Data2 = [B, B, B, B, B, B, B]
Data1 = [N, E, N, B, N, N, N]
data = [Data6,
Data5,
Data4,
Data3,
Data2,
Data1]
new_data = []
for dt in data:
new_data = new_data + dt
print (new_data)
Upvotes: 0
Reputation: 169
use sum with init value []
data = sum(data, [])
or build like this:
data = [*Data6, *Data5, *Data4, *Data3, *Data2, *Data1]
Upvotes: 2
Reputation: 11917
You can just use extend like this
N, B, E = "N", "B", "E"
Data6 = [N, N, N, N, N, N, N]
Data5 = [N, N, N, N, N, N, N]
Data4 = [N, N, N, B, E, N, E]
Data3 = [N, N, N, N, B, E, N]
Data2 = [B, B, B, B, B, B, B]
Data1 = [N, E, N, B, N, N, N]
data = []
data.extend(Data6)
data.extend(Data5)
data.extend(Data4)
data.extend(Data3)
data.extend(Data2)
data.extend(Data1)
print(data) # Output : ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'B', 'E', 'N', 'E', 'N', 'N', 'N', 'N', 'B', 'E', 'N', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'N', 'E', 'N', 'B', 'N', 'N', 'N']
Upvotes: 1
Reputation: 163
You should use the +
operator for lists.
l1 = [1,2,3,4]
l2 = [5,6,7,8]
print(l1+l2)
>> [1,2,3,4,5,6,7,8]
This operation is called concatenation.
What you want for your problem is :
data = Data6 + Data5 + Data4 + Data3 + Data2 + Data1
Upvotes: 2