Reputation: 11
How can I merge multiple tables in python using tabulate package?
append
is not working when concatenating the two tables in python.The tables are implemented using tabulate package in python.
table_1 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
table_2 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
table_3 = table_1.append(table_2)
print table_3
Upvotes: 1
Views: 2294
Reputation: 1
You can apply append because the values will be in list format..........
table_1 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]] table_2 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]] table_1.extend(table_2) print table_1
Upvotes: 0
Reputation: 7380
For " concatenating the two tables" you probably want list.extend, not append. Append will insert the second list as a single item to the first one. Also note that extend
will modify the source list in-place and return None
:
>>> table_1 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
>>> table_2 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
>>> table_3 = table_1.extend(table_2)
>>> print table_3
None
table_3
is now None
, while table_1
was extended:
>>> print table_1
[['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3], ['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3]]
Upvotes: 3
Reputation: 34027
list.append
or extend
mutates the list inplace and returns None
, so here your table_3
gets None
in it. You can use +
to add concatenate two lists:
In [251]: table_1 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
...: table_2 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
...: table_3 = table_1 + table_2
...: print table_3
[['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3], ['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3]]
Upvotes: 1