-1

I know different versions of this question has been answered for example [here].1 But I just couldn't seem to get it working on my example. I am trying to make a copy of ListA called ListB and then add an additional item to ListB i.e. 'New_Field_Name' in which later on I use as column headers for a dataframe.

Here is my code:

ListA = ['BUSINESSUNIT_NAME' ,'ISONAME', 'Planning_Channel', 'Planning_Partner', 'Is_Tracked', 'Week', 'Period_Number']
print type(ListA)

Output:

ListB = list(ListA)
print '######', ListB, '#####'

Output: ###### ['BUSINESSUNIT_NAME', 'ISONAME', 'Planning_Channel', 'Planning_Partner', 'Is_Tracked', 'Week', 'Period_Number'] #####

ListB = ListB.append('New_Field_Name')
print type(ListB)
print ListB

Output: None

Community
  • 1
  • 1
IcemanBerlin
  • 3,239
  • 6
  • 29
  • 33

1 Answers1

3

append does not return anything, so you cannot say

ListB = ListB.append('New_Field_Name')

It modifies the list in place, so you just need to say

ListB.append('New_Field_Name')

Or you could say

ListB += ['New_Field_Name']
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 3
    It's worth mentioning that it's a convention in Python that functions that modify a mutable object in-place return `None`. – PM 2Ring Jan 23 '15 at 12:43
  • Thanks Cyber, it is clear for me now. It has been flagged as a duplicate question although I this example highlights the issue in a more concise manner. – IcemanBerlin Jan 23 '15 at 12:49