Append in string and lists

While running this code:

j=‘aa’
l=‘bb’
m=‘cc’

new=
big=

new.append(j)
big.append(new)
print(new)
print(big)

new.append(l)
big.append(new)
print(new)
print(big)

new.append(m)
big.append(new)
print(new)
print(big)

the result will be:

new=[‘aa’]
big=[[‘aa’]]
new=[‘aa’, ‘bb’]
big=[[‘aa’, ‘bb’], [‘aa’, ‘bb’]]
new=[‘aa’, ‘bb’, ‘cc’]
big=[[‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’]]

i get this: why it get appended like this([[‘aa’, ‘bb’], [‘aa’, ‘bb’]]) instead of ([[‘aa’], [‘aa’, ‘bb’]])

1 Like

Hi,

Sorry if I don’t fully understand what the question is. But I’ll try to answer. :slight_smile:

When you append to a list, you append the entire object. So when you big.append(new), new is already a list, so that’s why you get the brackets inside the brackets (list object inside a list).

If you just want to append a specific list item you need to specify its index number, like this big.append(new[indexNumber]) where index number refers to the index of the item in the list you want to append.

That might look like
big.append(new[0])

1 Like

At the very end

big = [['aa', 'bb', 'cc'], ['aa', 'bb', 'cc']]
new = ['aa', 'bb', 'cc']
big.append(new)
>>> [['aa', 'bb', 'cc'], ['aa', 'bb', 'cc'], ['aa', 'bb', 'cc']]

Now look at the results:

Actually in the 3rd line in the result, as i appended (l='bb")in new which already contain[‘aa’], the newer new is [‘aa’, ‘bb’]
but, if i append the newer new to the big, why am i not getting result like this in the 4th line of result(big=[[‘aa’], [‘aa’, ‘bb’]]) but instead got (big=[[‘aa’, ‘bb’], [‘aa’, ‘bb’]])

no, it was like this:
big = [[‘aa’, ‘bb’, ], [‘aa’, ‘bb’]]
new = [‘aa’, ‘bb’, ‘cc’]
big.append(new)

[[‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’]]

1 Like

You can be clever about string formatting to see what’s happening under the hood @davyjones28. It’s a very useful technique :sweat_smile:

j='aa'
l='bb'
m='cc'

new=[]
big=[]
print('Appending {} to {}'.format(j, new))
new.append(j)
print('result is {}'.format(new))
print('*********')
print('Appending {} to {}'.format(new, big))
big.append(new)
print('result is {}'.format(big))
print('*********')
print('Appending {} to {}'.format(l, new))
new.append(l)
print('result is {}'.format(new))
print('*********')
print('Appending {} to {}'.format(new, big))
big.append(new)
print('result is {}'.format(big))
print('*********')
print('Appending {} to {}'.format(m, new))
new.append(m)
print('result is {}'.format(new))
print('*********')
print('Appending {} to {}'.format(new, big))
big.append(new)


print('result is {}'.format(big))
print('*********')

Appending aa to
result is [‘aa’]


Appending [‘aa’] to
result is [[‘aa’]]


Appending bb to [‘aa’]
result is [‘aa’, ‘bb’]


Appending [‘aa’, ‘bb’] to [[‘aa’, ‘bb’]]
result is [[‘aa’, ‘bb’], [‘aa’, ‘bb’]]


Appending cc to [‘aa’, ‘bb’]
result is [‘aa’, ‘bb’, ‘cc’]


Appending [‘aa’, ‘bb’, ‘cc’] to [[‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’]]
result is [[‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’]]


Appending aa to
result is [‘aa’]

Appending [‘aa’] to
result is [[‘aa’]] >>till here it"s okay

Appending bb to [‘aa’]
result is [‘aa’, ‘bb’]

Appending [‘aa’, ‘bb’] to [[‘aa’, ‘bb’]] >> In this line we are appending [‘aa’, ‘bb’] to . [[‘aa’]] (old result) but you are appending to . . [[‘aa’, ‘bb’]] …HOW???
result is [[‘aa’, ‘bb’], [‘aa’, ‘bb’]]

Appending cc to [‘aa’, ‘bb’]
result is [‘aa’, ‘bb’, ‘cc’]

Appending [‘aa’, ‘bb’, ‘cc’] to [[‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’]]
result is [[‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’], [‘aa’, ‘bb’, ‘cc’]]

Start with this [[‘aa’, ‘bb’]]. Let x = [‘aa’, ‘bb’]

So [[‘aa’, ‘bb’]] becomes [x].

Now we say “Appending [‘aa’, ‘bb’] to” [x]. Which can be written "Appending x to [x]"

We get [x, x], which is the same as [[‘aa’, ‘bb’], [‘aa’, ‘bb’]]

Which is why it is proper for the program to say

Appending [‘aa’, ‘bb’] to [[‘aa’, ‘bb’]]
result is [[‘aa’, ‘bb’], [‘aa’, ‘bb’]]

You ask a very good question. Again if one digs deeper there’s a clue

Appending J: aa to NEW:
result is NEW:[‘aa’]
********* 1st round
Appending NEW: [‘aa’] to BIG:
result is BIG: [[‘aa’]]
********* 2nd round
Appending L: bb to NEW: [‘aa’]
result is NEW: [‘aa’, ‘bb’]
********* 3rd round
Appending NEW: [‘aa’, ‘bb’] to BIG: [[‘aa’, ‘bb’]]
result is BIG: [[‘aa’, ‘bb’], [‘aa’, ‘bb’]]

Why is big [[‘aa’, ‘bb’]] now?? Shouldn’t it be [[‘aa’]]?

The answer is that big is actually [new], so when new = [‘aa’, ‘bb’], big = [[‘aa’, ‘bb’]].

@davyjones28 if you remember our previous example I let the value = x. This is what python is actually interpreting when you pass these variables around.

1 Like

Appending J: aa to NEW:
result is NEW:[‘aa’]
********* 1st round
Appending NEW: [‘aa’] to BIG:
result is BIG: [NEW]
********* 2nd round
Appending L: bb to NEW: [‘aa’]
result is NEW: [‘aa’, ‘bb’]
********* 3rd round
Appending NEW: [‘aa’, ‘bb’] to BIG: [NEW] or [ [‘aa’, ‘bb’] ]
result is BIG: [[‘aa’, ‘bb’], [‘aa’, ‘bb’]]

I understood that, but my question is if i want the final big value to be like this:
[[‘aa’], [‘aa’, ‘bb’], [‘aa’, ‘bb’, ‘cc’]]

then what will be the code for this

@davyjones28

When you set a variable equal another variable, it will track it’s change. If you want to avoid this, one alternative is to set it equal to a copy of itself. x = y.copy()

1 Like