2

i have two nested list like:

a = [[2,3,4],[3,5,6]]
b = [[4,5], [5,6,7,7,7]]

i need to append two nested list into single nested list.

Expected output:

[[4, 5], [5, 6, 7, 7, 7], [2, 3, 4], [3, 5, 6]]

I tried this way,

a = [[2,3,4],[3,5,6]]
b = [[4,5], [5,6,7,7,7]]
b.append(a)
print(b)

output i got:

[[4, 5], [5, 6, 7, 7, 7], [[2, 3, 4], [3, 5, 6]]]

Any suggestions would be helpful!

1
  • 1
    Use extend method of the list object. Commented Mar 10, 2020 at 14:19

3 Answers 3

2

Just create a new list:

a = [[2,3,4],[3,5,6]]
b = [[4,5], [5,6,7,7,7]]
c = a + b
# [[2, 3, 4], [3, 5, 6], [4, 5], [5, 6, 7, 7, 7]]
Sign up to request clarification or add additional context in comments.

Comments

2

Unpacking is one way of doing it:

c = [*a, *b]
# [[2, 3, 4], [3, 5, 6], [4, 5], [5, 6, 7, 7, 7]]

Comments

1

Use .extend, given

a = [[2,3,4],[3,5,6]]
b = [[4,5], [5,6,7,7,7]]
b.extend(a)

Note: the .extend method extends the existing list and changes made are in the list on which .extend is performed, so here changes are made to b

output:

[[4, 5], [5, 6, 7, 7, 7], [2, 3, 4], [3, 5, 6]]

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.