noob problem: different output to similar code

This is the place for queries that don't fit in any of the other categories.

noob problem: different output to similar code

Postby vinci » Mon May 13, 2013 11:02 am

Hello, I'm quite new at python and I'm currently doing the learn the hard way python tutorial. Now I'm doing ex 32 (http://learnpythonthehardway.org/book/ex32.html) where I have the following code to play with:
Code: Select all
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print "This is count %d" % number

# same as above
for fruit in fruits:
    print "A fruit of type: %s" % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print "I got %r" % i

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append is a function that lists understand
    elements.append(i)

# now we can print them out too
for i in elements:
    print "Element was: %d" % i



I'm interested in the last part, beginning with "elements = []". This is the normal expected output:
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

But when I try to write this part in python (both as a separate script and as lines executed within python), the output is quite different, and I don't really understand why. This is what I've written:
Code: Select all
elements = []
for i in range (0,6):
    print "this is the range:    %d" %i
elements.append(i)

for i in elements:
    print "this is the new list    %d" %i


The first part is basically the same thing:
this is the range: 0
this is the range: 1
this is the range: 2
this is the range: 3
this is the range: 4
this is the range: 5

and the next line is:
this is the new list 5


So, instead of outputing 6 lines beginning with 0, it only says '5', and I don't know what I am doing wrong. So I'd appreciate your help regarding this problem.

P.S. I hope this is not the wrong thread to post in.
vinci
 
Posts: 2
Joined: Mon May 13, 2013 10:47 am

Re: noob problem: different output to similar code

Postby Kebap » Mon May 13, 2013 1:11 pm

Hello and welcome to python! :) Good job with the tutorial.

You see, indentation is very important in python. Now the line in your code, where you append contents to your list called "elements", it is only called once. So there is only one element in that list. So there is only one line printed. As you may notice, the code does exactly what you tell it to. Unless, you actually wanted to tell it something else. Try indenting that line, too, so that it is inside the loop, too, so that all elements will be added and not only the one after the loop, and notice the differences there. Hope this helps, otherwise feel free to ask more! :mrgreen:

edit: first! ha
Last edited by Kebap on Mon May 13, 2013 1:27 pm, edited 2 times in total.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: noob problem: different output to similar code

Postby stranac » Mon May 13, 2013 1:13 pm

vinci wrote:
Code: Select all
elements = []
for i in range (0,6):
    print "this is the range:    %d" %i
elements.append(i)

Note how the last line is not indented. That means it's not a part of the loop.
So, elements.append(i) is only executed once, i being 5, after the loop finishes.

If you indent that line, you will probably get the results you expected.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1115
Joined: Thu Feb 07, 2013 3:42 pm

Re: noob problem: different output to similar code

Postby vinci » Mon May 13, 2013 2:30 pm

Thank you both for your replies. Of course I knew the principle, but, as I'm still not familiar enough with coding, I'm bound do make these types of errors.

And while we're at it, I wanted to ask another question regarding lists. I know that appending a list to another creates a sublist within a list. For instance, if:

Code: Select all
list1 = [1, 2, 3]
list2 = [4, 5, 6]


and append list2 to list1, that is list1.append(list2), then the result will be: [1, 2, 3, [4, 5, 6]], instead of [1, 2, 3, 4, 5, 6] (which is actually what happens in the code I had problems with) or, why not?, [[1, 2, 3], [4, 5, 6]]. Can you explain to me in short why this happens? How can I make python show the results I had expected by concatenating two lists?
vinci
 
Posts: 2
Joined: Mon May 13, 2013 10:47 am

Re: noob problem: different output to similar code

Postby Mekire » Mon May 13, 2013 2:42 pm

If you append an object to a list you are telling it to place that object in a new index on the end of the list. It doesn't matter if the item you append to the list is another list or an Artichoke; it will place it in the next slot on the list to which you are appending.

Code: Select all
class Artichoke:
    pass

mylist = [1,2,3]
otherlist = [4,5,6]

MyArtichoke = Artichoke()

mylist.append(MyArtichoke)
print(mylist)

mylist.append(otherlist)
print(mylist)
Code: Select all
>>>
[1, 2, 3, <__main__.Artichoke instance at 0x028A6DA0>]
[1, 2, 3, <__main__.Artichoke instance at 0x028A6DA0>, [4, 5, 6]]
>>>

-Mek

Edit:
Code: Select all
mylist = [1,2,3]
other_list = [4,5,6]

new_list = []
new_list.append(mylist)
new_list.append(other_list)

print(new_list)
Code: Select all
>>>
[[1, 2, 3], [4, 5, 6]]
>>>
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: noob problem: different output to similar code

Postby DrakeMagi » Mon May 13, 2013 2:57 pm

to add list to a list. You extend a list
Code: Select all
list1 = [1,2,3]
list2 = [4,5,6]
list1.extend(list2)
print list1
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 106
Joined: Sun May 12, 2013 8:36 pm

Re: noob problem: different output to similar code

Postby Kebap » Mon May 13, 2013 3:08 pm

Then again, there are many more funny things you can do with lists.

Code: Select all
>>> [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: noob problem: different output to similar code

Postby Mekire » Mon May 13, 2013 3:24 pm

DrakeMagi wrote:to add list to a list. You extend a list

I don't know why it would be the case as extend is provided as a built-in, but at least from my experimentation, concatenation seems to always edge out extend.

Code: Select all
import timeit

def do_it_extend():
    a = list(range(10000))
    b = list(range(10000,20000))
    a.extend(b)

def do_it_concatenate():
    a = list(range(10000))
    b = list(range(10000,20000))
    a += b

times = 1000
T = timeit.Timer("do_it_extend()","from __main__ import do_it_extend")
print(T.timeit(times)/times)

T = timeit.Timer("do_it_concatenate()","from __main__ import do_it_concatenate")
print(T.timeit(times)/times)

-Mek

Edit: And yes I understand that extend is operating in place. I just don't see the value of it.
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 3 guests