6

I want to combine two lists into a list of lists. And vice versa. I can't find anything seems to work and I am very new to Python

Example:

S1 = [1,2,3]
S2 = [4,5,6,7]

Expected output 
S = [[1,2,3],[4,5,6,7]]

and How can I split S back into original S1 and S2 ? Example:

S = [[1,2,3],[4,5,6,7]]
Expected output
S1 = [1,2,3]
S2 = [4,5,6,7]
1
  • These are the absolute basics of working with lists, covered as the very first thing in any tutorial's section about lists. This site expects you to do research before asking a question, as we are not your private tutoring service. Commented Apr 9, 2017 at 3:13

1 Answer 1

8

This is the simplest solution.

>>> S1 = [1,2,3]
>>> S2 = [4,5,6,7]
>>> S = [S1, S2]
>>> S
[[1, 2, 3], [4, 5, 6, 7]]

To get your lists back again:

>>> S1 = S[0]
>>> S2 = S[1]
>>> S1
[1, 2, 3]
>>> S2
[4, 5, 6, 7]
Sign up to request clarification or add additional context in comments.

2 Comments

As stated in How to Answer, please avoid answering unclear, broad, SW rec, typo, opinion-based, unreproducible, or duplicate questions. Write-my-code requests and low-effort homework questions are off-topic for Stack Overflow and more suited to professional coding/tutoring services. Good questions adhere to How to Ask, include a minimal reproducible example, have research effort, and have the potential to be useful to future visitors. Answering inappropriate questions harms the site by making it more difficult to navigate and encouraging further such questions, which can drive away other users who volunteer their time and expertise.
Thank you much!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.