-3

So I have a tuple that has two lists in it:

x = (['a', 's', 'd'], ['1', '2', '3'])

How do I make it into two lists? Right now my code basically looks like:

list1.append(x[1])
list1.append(x[2])
list1.append(x[3])

But I can't add the other 3 items into a separate list with indexes 4, 5 and 6:

list2.append(x[4])
list2.append(x[5])      -results in an error
list2.append(x[6])

How can I do the above to make a list2?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
S. Martin
  • 1
  • 1

1 Answers1

2

Your tuple has only two elements. Just reference those directly:

list1 = x[0]
list2 = x[1]

or

list1, list2 = x

This creates additional references to the two lists contained in x, not copies. If you need new list objects with the same contents, create copies:

list1, list2 = x[0][:], x[1][:]

See How to clone or copy a list?

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks! I'm still new at this so I automatically tried appending with indexes 1, 2 and 3, which gave me the correct answer for list1, but didn't work for list2 – S. Martin Oct 22 '15 at 12:49
  • 2
    @S.Martin: but you don't *have* and index `2` in your tuple, as defined in the question. If those indices worked, then `x` is a different object and you asked the wrong question. – Martijn Pieters Oct 22 '15 at 12:51