5

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]
user02
  • 281
  • 3
  • 10
  • 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. – TigerhawkT3 Apr 09 '17 at 03:13

1 Answers1

7

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]
Remolten
  • 2,614
  • 2
  • 25
  • 29
  • 2
    As stated in [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 [so] and more suited to professional coding/tutoring services. Good questions adhere to [ask], include a [mcve], 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. – TigerhawkT3 Apr 09 '17 at 03:12
  • Thank you much!! – user02 Apr 09 '17 at 03:20