473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

list1.append(list2) returns None

def enlargetable(table,col):
return table.append(col)

def removecolfromtable(table,col):
return table.remove(col)

print enlargetable([[1],[2],[3]],[4]) # returns None

Why does it return None instead of [[1],[2],[3],[4]] which I expected?
Dec 21 '06 #1
9 2277
Pyenos wrote:
def enlargetable(table,col):
return table.append(col)

def removecolfromtable(table,col):
return table.remove(col)

print enlargetable([[1],[2],[3]],[4]) # returns None

Why does it return None instead of [[1],[2],[3],[4]] which I expected?
append modifies the list and then returns None:
>>print a
[1, 2, 3]
>>print a.append(4)
None
>>print a
[1, 2, 3, 4]
The reasoning given at
http://www.python.org/doc/faq/genera...he-sorted-list
is so you wont do something like this:

a = [1,2,3]
b = a.append(4)

and assume that a is still [1,2,3]
More discussion on this topic is available at
http://groups.google.com/group/comp....b2e67550123b92

Todd

Dec 21 '06 #2

Pyenos wrote:
def enlargetable(table,col):
return table.append(col)

def removecolfromtable(table,col):
return table.remove(col)

print enlargetable([[1],[2],[3]],[4]) # returns None

Why does it return None instead of [[1],[2],[3],[4]] which I expected?
return the table. Ex:

def enlargetable(table, col):
table.append(col)
return table

Dec 21 '06 #3
def enlargetable(table,col):
return table.append(col)
Google for "python pitfalls"

--
Gabriel Genellina

Dec 21 '06 #4
Pyenos <py****@pyenos.orgwrites:
def enlargetable(table,col):
return table.append(col)

def removecolfromtable(table,col):
return table.remove(col)

print enlargetable([[1],[2],[3]],[4]) # returns None

Why does it return None instead of [[1],[2],[3],[4]] which I expected?
The answer is both "because that's what it's documented to do":

s.append(x) same as s[len(s):len(s)] = [x]
<URL:http://docs.python.org/lib/typesseq-mutable.html>

and "because you asked it to *do* something, not *get* something":

<URL:http://www.python.org/doc/faq/general.html#why-doesn-t-list-sort-return-the-sorted-list>

--
\ "There was a point to this story, but it has temporarily |
`\ escaped the chronicler's mind." -- Douglas Adams |
_o__) |
Ben Finney

Dec 21 '06 #5
On Thu, 21 Dec 2006 16:29:01 +1100, Ben Finney wrote:
Pyenos <py****@pyenos.orgwrites:
>def enlargetable(table,col):
return table.append(col)

def removecolfromtable(table,col):
return table.remove(col)

print enlargetable([[1],[2],[3]],[4]) # returns None

Why does it return None instead of [[1],[2],[3],[4]] which I expected?

The answer is both "because that's what it's documented to do":
Documentation is a funny thing...

help([].append)

Help on built-in function append:

append(...)
L.append(object) -- append object to end

Sometimes it is hard to tell when you've read enough documentation.
However, as a general rule, "None whatsoever" is rarely enough.

--
Steven

Dec 21 '06 #6
i rewrote the code following the advices from subdir of the parent thread:

# well, table is composed of a list of columns.
# so let's stick them together
def enlargetable(table,col):
table.append(col) # the code won't return sorted lists :-o
return_the_fucking_table_bitch=table # assign it to a new variable to return it
return return_the_fucking_table_bitch # :-|
def removecolfromtable(table,col):
return table.remove(col)

print enlargetable([[1],[2],[3]],[4])

# and this code works.
Dec 21 '06 #7
Pyenos schrieb:
i rewrote the code following the advices from subdir of the parent thread:

# well, table is composed of a list of columns.
# so let's stick them together
def enlargetable(table,col):
table.append(col) # the code won't return sorted lists :-o
Why should it sort the list?
return_the_fucking_table_bitch=table # assign it to a new variable to return it
return return_the_fucking_table_bitch # :-|
That isn't necessary. A simple "return table" is fine.
>
def removecolfromtable(table,col):
return table.remove(col)
table.remove() also returns None.
print enlargetable([[1],[2],[3]],[4])

# and this code works.
Georg
Dec 21 '06 #8

Pyenos wrote:
def enlargetable(table,col):
table.append(col) # the code won't return sorted lists :-o
return_the_fucking_table_bitch=table # assign it to a new variable to return it
return return_the_fucking_table_bitch # :-|
Maybe you were just trying to be funny, but assiging the table to
another variable before returning is not only unnecessary, it
accomplishes nothing. The following will do exactly the same thing as
the above:

Expand|Select|Wrap|Line Numbers
  1. def enlargetable(table,col):
  2. table.append(col)
  3. return table
  4.  
Dec 21 '06 #9
At Thursday 21/12/2006 14:50, Matimus wrote:
>The following will do exactly the same thing as
the above:

Expand|Select|Wrap|Line Numbers
  1. def enlargetable(table,col):
  2.     table.append(col)
  3.     return table
Which, by the way, was one of the first answers he got, by Edward Kozlowski.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Dec 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jakle | last post by:
I'm trying to write a program (with my very limited knowledge of python) that will convert text I type into those letters drawn with ascii symbols. I did 2 letters then went to test it. Here's the...
4
by: Klaus Neuner | last post by:
Hello, the function given below returns all indexes of list2 where a sublist of list2 that is identical to list1 begins. As I will need this function quite often, I would like to know if more...
4
by: Bill | last post by:
I am trying to do an exercise in an online tutorial that but the tutorial does not list the solution. Could someone please tell me why the "hireCrew" method in the following code leaves me with a...
6
by: M. Clift | last post by:
Hi All, I have tried to come up with a way to do this myself and all I end up with is very long code. What I have is a say item1, item4, item2, item1 etc... What I want to do is append to...
30
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None,...
5
by: randomtalk | last post by:
hello, recently i tried to use list.append() function in seemingly logical ways, however, i cannot get it to work, here is the test code: >>> seed = >>> next = 7 >>> seed1 = seed.append(next)...
4
by: pmacdiddie | last post by:
I have an append query that needs to run every time a line item is added to a subform. The append writes to a table that is the source for a pull down box. Problem is that it takes 5 seconds to...
7
by: Tobiah | last post by:
I wanted to do: query = "query text" % tuple(rec.append(extra)) but the append() method returns none, so I did this: fields = rec fields.append(extra) query = "query text" % tuple(fields)
42
by: Armin | last post by:
Hi, just a dumb question. Let a = Why is the value of a.append(7) equal None and not ?? --Armin
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.