0

I want to append the lists "b", "c" and "d" to my list "a". Then i want to print the single lists in each line. My expected output is:

[(4, 'Blue'), (3, 'Red'), (4, 'Red')]
[(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
[(1, 'Green'), (3, 'Blue'), (3, 'Green')]
[(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]

My current code is:

a = [(4, 'Blue'), (3, 'Red'), (4, 'Red')]
b = [(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
c = [(1, 'Green'), (3, 'Blue'), (3, 'Green')]
d = [(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]

a.append(b)
a.append(c)
a.append(d)

for x in a:
    print(x)

My current output is:

(4, 'Blue')
(3, 'Red')
(4, 'Red')
[(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
[(1, 'Green'), (3, 'Blue'), (3, 'Green')]
[(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]

As you see the first 3 lines of output are separated and not together. How can I change this so that it appears according to my expected output?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Tamaron
  • 19
  • 2
  • 2
    Do you want a list of lists or a list of elements? – Christian Dean Nov 25 '21 at 15:38
  • You can use `a.extend([b, c, d])` instead of appending 3 times. – Jonas Palačionis Nov 25 '21 at 15:41
  • 1
    Don't you just need `e = [a,b,c,d]`? – Sayse Nov 25 '21 at 15:42
  • Sorry about the initial duplicate marking. I misread your intended output. – Karl Knechtel Nov 25 '21 at 15:44
  • You don't need to use `.append` at all, as @Sayse points out. But you can easily fix the code by simply understanding what happened. Think: *why* does `a.append(b)` give you the initial elements of `a`, followed by `b` as a single element? Given that the appending of `b` puts the correct thing into your output, can you think of a way to put `a` into your output correctly? (By using `.append(a)` on something, right?) If you were to do that, what starting point would you need to have instead? (it would have to be a separate thing, right, Which is empty? Yeah?) – Karl Knechtel Nov 25 '21 at 15:47

3 Answers3

0

Your current code adds each b/c/d list as an element. So you have a list with 6 element, the last 3 of which are lists.

[(4, 'Blue'),
 (3, 'Red'),
 (4, 'Red'),
 [(2, 'Green'), (4, 'Green'), (1, 'Yellow')],
 [(1, 'Green'), (3, 'Blue'), (3, 'Green')],
 [(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]]

I think what you want to do is use a larger container. (WHY? I don't know, I guess it's a dummy example part of a larger code).

a = [(4, 'Blue'), (3, 'Red'), (4, 'Red')]
b = [(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
c = [(1, 'Green'), (3, 'Blue'), (3, 'Green')]
d = [(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]

out = [a]
out.append(b)
out.append(c)
out.append(d)
# or out = [a,b,c,d]
# or out.extend([b,c,d])

for x in out:
    print(x)

output:

[(4, 'Blue'), (3, 'Red'), (4, 'Red')]
[(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
[(1, 'Green'), (3, 'Blue'), (3, 'Green')]
[(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]
mozway
  • 194,879
  • 13
  • 39
  • 75
0

The problem here is that you are appending b, c, and d as elements to a, which means that the original elements of a are not nested in a list like that of b, c, and d.

To package the elements of a into a list first, we can do a = [a] before the appending.

So our code now becomes:

a = [(4, 'Blue'), (3, 'Red'), (4, 'Red')]
b = [(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
c = [(1, 'Green'), (3, 'Blue'), (3, 'Green')]
d = [(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]

a = [a]
a.append(b)
a.append(c)
a.append(d)

for x in a:
    print(x)

Output:

(4, 'Blue'), (3, 'Red'), (4, 'Red')]
[(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
[(1, 'Green'), (3, 'Blue'), (3, 'Green')]
[(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]
fwoosh
  • 184
  • 3
  • 15
-3

My expected output should be:

[(4, 'Blue'), (3, 'Red'), (4, 'Red')]
[(2, 'Green'), (4, 'Green'), (1, 'Yellow')]
[(1, 'Green'), (3, 'Blue'), (3, 'Green')]
[(2, 'Blue'), (1, 'Red'), (4, 'Yellow')]

That's not a valid list. It's just four different lists.

oittaa
  • 599
  • 3
  • 16
  • Taramon is referring to the output as generated by the `for` loop, not the list itself – fwoosh Nov 25 '21 at 15:43
  • It is valid, the op is printing the lists – Sayse Nov 25 '21 at 15:43
  • The output comes from iterating over a list and `print`ing each element. Clearly what OP is saying is that the resulting data should be a list of lists, but the problem is that it instead contains some elements from the first list mixed with the other lists. – Karl Knechtel Nov 25 '21 at 15:43