0

I have nested tuple like below wanted to convert into nested list in the format mentioned

Input:

T = [('id','1'),('name','Mike'),('Adrs','Tor')]

Output:

L = [['id','1'],['name','Mike'],['Adrs','Tor']]
7
  • L.append(list(item))? Commented Mar 25, 2021 at 19:18
  • Hi , It errors out as list object is not callable. Commented Mar 25, 2021 at 19:20
  • It works for me at least. What error are you receiving excactly? Commented Mar 25, 2021 at 19:22
  • 1
    that error means that probably you have used name list and overriden the built-in function list(). Don't use list as variable name. Commented Mar 25, 2021 at 19:23
  • Same 'TypeError "list' object is not callable Commented Mar 25, 2021 at 19:23

3 Answers 3

1
>>> spam = [('id','1'),('name','Mike'),('Adrs','Tor')]
>>> eggs = [list(item) for item in spam]
>>> eggs
[['id', '1'], ['name', 'Mike'], ['Adrs', 'Tor']]
Sign up to request clarification or add additional context in comments.

Comments

0

You could unpack each separate tuple and then append it

L = []

for item in T:
    a, b = item
    L.append([a,b])

Comments

0
l = [('id','1'),('name','Mike')]
m = [[], []]
for i in range(len(l)):
    m[i] = [l[i][0], l[i][1]]
print(m)

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

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.