0

I have a list of tuples (a) that i want to change to list of lists (b).

a = [('ad','as'),('bs','bt'),('cs','cr')]
b = [['ad','as'],['bs','bt'],['cs','cr']]

I tried the code below but it does not change anything. Am i missing something?

b = []
for element in a:
    b.append(list(element))
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52

4 Answers4

2

You can try with a shorter solution like:

b = map(list,a)

This generates a new list by applying the list function to every single element of 'a'.

This map is equivalent to the relatively longer code using comprehension lists:

  b = [ list(x) for x in a]
0
a = [('ad','as'),('bs','bt'),('cs','cr')]
b = [[elem[0], elem[1]] for elem in a]
Josip Grggurica
  • 421
  • 4
  • 12
0
>>>a = [('ad','as'),('bs','bt'),('cs','cr')]
>>>b = [list(item) for item in a]
>>>b
[['ad', 'as'], ['bs', 'bt'], ['cs', 'cr']]
Kousik
  • 21,485
  • 7
  • 36
  • 59
-1
a = [('ad','as'),('bs','bt'),('cs','cr')]
b = []
for element in a:
    b.append(list(element))
print(b)

shows that b is now the desired list of lists - so your code works fine...

AND: Of course it does not change anythin if you really define bas shown in your example because:

  1. you first define bas desired
  2. You create a new list (empty) and label it b
  3. you fill this list with the desired values.

--> the new b is the same as the old b - therefore there are no visible changes even though your code works.

If you want to add the tuple elements a second time, just delete the b = [] line.

OBu
  • 4,977
  • 3
  • 29
  • 45