gaggio
gaggio

Reputation: 545

Concatenate a list with a list of lists

I need to extend a list with a variable number of lists as in the following example:

a = [1, 2, 3]
b = [4, 5]
c = [6, 7, 8, 9]
d = [b, c]

The command I'm looking for should operate on a and d, and should give the following output:

[[1, 2, 3], [4, 5], [6, 7, 8, 9]]

that is the same as the output of:

[a, b, c]

As a bonus, it would be good if the same command could operate with d defined as (b, c).

Upvotes: 1

Views: 198

Answers (3)

NPE
NPE

Reputation: 500167

In [6]: [a] + d
Out[6]: [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

To make it work when d is a tuple, change that to

[a] + list(d)

Upvotes: 3

Vidac
Vidac

Reputation: 83

The output of [a,b,c] should be [[1, 2, 3], [3, 4], [5, 6, 7, 8]] in your example. Is this what you want ? If so, doing e = [a,]+list(d) should work.

Upvotes: 0

jramirez
jramirez

Reputation: 8685

In [20]: d.insert(0,a)

In [21]: d
Out[21]: [[1, 2, 3], [3, 4], [5, 6, 7, 8]]

Upvotes: 1

Related Questions