1

Let's suppose that I have 3 python two-dimensional lists (data_1, data_2, data_3) of 10x5 dimensions. I want to make one list (all_data) from them with 30x5 dimensions. For now, I am doing this by applying the following:

data_1 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], ..., [46, 47, 48, 49, 50]]
data_2 = [[101, 102, 103, 104, 105], [106, 107, 108, 109, 110], ..., [146, 147, 148, 149, 150]]
data_3 = [[201, 202, 203, 204, 205], [206, 207, 208, 209, 210], ..., [246, 247, 248, 249, 250]]
all_data = []
for index, item in enumerate(data_1):
    all_data.append(item)

for index, item in enumerate(data_2):
    all_data.append(item)

for index, item in enumerate(data_3):
    all_data.append(item)

If I simply do something like this:

all_data.append(data_1)
all_data.append(data_2)
all_data.append(data_3)

then I get a list of 3x10x5 which is not what I want.

So can I create a 30x5 list by appending three 10x5 lists without using any for loop?

1

5 Answers 5

7

You can just extend your list.

data = []
data.extend(d1)
data.extend(d2)
data.extend(d3)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response but this produces a 10x15 list and not a 30x5 list from the initial lists...
No, it does not (like every answer you commented on). Consider sharing your list.
5

Simply write:

all_data = data_1 + data_2 + data_3

If you want to merge all the corresponding sublists of these lists, you can write:

# function that merges all sublists in single list
def flatten(l):
    return [el for subl in l for el in subl]

# zipped - is a list of tuples of corresponding items (our deep sublists) of all the lists
zipped = zip(data_1, data_3, data_3)

# merge tuples to get single list of rearranges sublists
all_data = flatten(zipped)

4 Comments

Thank you for your response but this produces a 10x15 list and not a 30x5 list from the initial lists...
if data_1 and data_2 and data_3 contains sublists, what dimension have data_1? And what dimension have it's sublist?
Thank you for completing the question. I added an alternative option to my answer. But I think that you are somewhere wrong about the dimensions of the desired list. The first option should give a dimension of 30 * 5.
Thank you for your correct response again (two upvotes).
1

How about:

all_data = data_1 + data_2 + data_3

9 Comments

Thank you for your response but this produces a 10x15 list and not a 30x5 list from the initial lists...
@Universalis can you provide me with an example, please? Cuz what I've posted does what you've described up there - namely put the elements of the 3 lists(be they lists or not) into one list. How does a "10x5" list look like?
I think he doesn't even know how his lists look like.
@jsmolka it's very simple to run his code across any of the answers and compare the output... anyway.
Those are just one dimensional list, there is nothing two dimensional about them.
|
0

This should work:

data=[]
data[len(data):] = data1
data[len(data):] = data2
data[len(data):] = data3

Comments

0

In case you don't mind it being lazy, you could also do

import itertools

all_data = itertools.chain(data1, data2, data3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.