Question

t2=("A","B","Hello") 
alist=["Barney",["Wilma"]] 
alist.append(t2)

print(alist.pop(1).extend(t2)) 
print(alist) 
print((alist[1][2].upper())) 
print(alist)

Hi guys. I just had a qs regarding tuples as I am new to python.

  1. why does print(alist.pop(1).extend(t2)) give NONE on execution?

  2. Why does print(a list) below that give ['Barney', ('A', 'B', 'Hello')] and not open tuple inside list as the function used is extend and not append?

    alist=["Hello"] 
    t1=(1,2,[3,4],5,6)
    t1[2].append(alist) 
    print(t1) 
    t1[2].pop().append(t1) 
    print(a list)
    

qs- Why does print(a list) in the end print ['Hello', (1, 2, [3, 4], 5, 6)]? is it because the thing that is pooled in the previous sentence i.e. Hello is alist?

Was it helpful?

Solution

  1. list.extend extends the list in place (see the docs: http://docs.python.org/2/tutorial/datastructures.html) , and thus returns none. Rather than having a new variable with the extended list, your list with the new items is still called alist

  2. alist prints ['Barney', ('A', 'B', 'Hello')], because that's what you've asked it to do. Step by step,

t2=("A","B","Hello")

alist=["Barney",["Wilma"]] 

alist.append(t2) # ['Barney', ['Wilma'], ('A', 'B', 'Hello')]

pop1 = alist.pop(1) # ['Wilma']
extend1 = pop1.extend(t2) #extend1 = None, pop1 = ['Wilma', 'A', 'B', 'Hello']
print(alist.pop(1).extend(t2)) # None
print(alist) #you've popped wilma, so the rest remains:   ['Barney', ('A', 'B', 'Hello')]

As for third, this should be exactly the behavior you expect. First, you pop hello from alist, then you append all of t1 to the thing you just popped, namely hello. So, ['Hello', (1, 2, [3, 4], 5, 6)] should be what you expected to get. Did you expect something different?

print(alist) on the last line prints ['Hello', (1, 2, [3, 4], 5, 6)], because when you run t1[2].pop(), what you get is a a reference to alist in particular, not an arbitrary list element. So then, the append(t1) appends t1 to the actual alist, which you then print.

OTHER TIPS

I'll try answering your first 2 questions:

  1. alist.pop(1).extend(t2) works like this:

    -> alist.pop(1) returns ["Wilma"] and alist becomes ['Barney', ('A', 'B', 'Hello')]

    -> ["Wilma"].extend(t2) returns None and only extends the temporary list without modifying alist

  2. print(alist) shows ['Barney', ('A', 'B', 'Hello')] because earlier you had executed alist.append(t2)

Lets see what your code does here:

After the third line, alist.append(t2), alist is ["Barney",["Wilma"], ("A","B","Hello")]. Now you pop (remove) ["Wilma"] from the list, and extend that list with the tuple t2. That list would then be ["Wilma","A","B","Hello"], but it's bound to no variable, and since extend is in-place the result of that operation is None, which is printed. And since the Wilma-sublist (including its extension) got removed from the list, alist is now ["Barney", ("A","B","Hello")].

So to answer your questions in short:

  1. Because the return value of extend is None
  2. Because extend was applied to the return value of pop, not to alist, but the tuple was appended to alist earlier in the code.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top