Currently working through tutorial "Python the hard way".
I'm learning about lists and loops (ex32).
At the end of the exercise Zed (Tutorial author) tells us to play around, which I have done.
# we can also build lists, first start with an empty one
elements = []
elements.append(range(0,6))
# then use the range function to do 0 to 5 counts
for element in elements:
print "Adding %s to elements" % element
# now we can print them out too
for element in elements:
print"Element was: %s" % element
This produces output like so:
Adding [0, 1, 2, 3, 4, 5] to elements
Element was: [0, 1, 2, 3, 4, 5]
I had expected to see something like this:
Adding 0 to elements
Adding 1 to elements
Adding 2 to elements
Adding 3 to elements
Adding 4 to elements
Adding 5 to elements
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5
But instead Python wants to print out my script in a oner, rather than the concatenated string with each list component.
I know that I could change the script to reflect the authors script exactly
# 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
but I'd just like to know why my piece does not work as expected?
you are appending a list to a list! you just want to create the list!
all you need to do is change the following:
elements = []
elements.append(range(0,6))
into
elements = range(0,6)
and you will get your expected results
when you created elements
for the first time, it was a blank list. Then you appended range(0,6)
to your empty list. Now elements looks like [[0,1,2,3,4,5]]
(or [range(0,6)]
), which is a list with one element, a list.
This is because elements
contains exactly one element
, which is a list
: [0, 1, 2, 3, 4, 5]
. list.append()
adds an item to the end of the list.
In [1]: elements = []
In [2]: elements.append(range(0,6))
In [3]: elements
Out[3]: [[0, 1, 2, 3, 4, 5]]
Perhaps you meant to extend the list:
In [1]: elements = []
In [2]: elements.extend(range(0, 6))
In [3]: elements
Out[3]: [0, 1, 2, 3, 4, 5]
Or replace it?
In [4]: elements = range(0,6)
In [5]: elements
Out[5]: [0, 1, 2, 3, 4, 5]
Or even:
In [6]: elements = [element for element in range(0,6)]
In [7]: elements
Out[7]: [0, 1, 2, 3, 4, 5]
The list comprehension is unnecessary in this example, but it demonstrates how it is easy to filter or map those elements.
.append
adds a single element to the list. That single element is range(0, 6)
, which is [0, 1, 2, 3, 4, 5]
(Johnsyweb got it in before me). You could use .extend
to append each one .
elements = []
elements.append(range(0,6))
# appends range(0,6) to elements. range(0,6) creates a list in Python 2.x but only in Python 2.x. thanks to adsmith for pointing this out.
print elements
[[0, 1, 2, 3, 4, 5]] # it's clear that elements has 1 element. A list.
that's why
for i in elements:
print "adding %s to elements" % i
produces:
adding [0,1,2,3,4,5] to elements
To get the output you want:
elements = range(0,6)
OR
elements = [i for i in range(0,6)] # list comprehension
then
for i in elements:
print "adding %s to elements" % i
outputs what you want
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.