1

I am running below code it's running.

for row in rows:
    print("%s:"%row[0])

Result:

Jan
Feb
Mar
Apr

Excepted result: 'Jan', 'Feb', 'Mar', 'Apr'

i am trying to insert data into list based on below code but i am getting an error.

lst = []
print(lst)
    for row in rows:
        print("%s"%row[0])
        lst =lst.append("%s"%row[0])
        print(lst)

i am run above code getting below result:

[]
Jan
None

AttributeError: 'NoneType' object has no attribute 'append'

How can i solve this?

Venkatesh Panabaka
  • 2,064
  • 4
  • 19
  • 27
  • 1. Don't use `list` as a variable name because that shadows the built-in `list` type. 2. The code in your 1st code block will _not_ produce the result you've shown. What happened to the `:` ? 3. The `list.append` method mutates the list object in-place and returns `None`. – PM 2Ring Jan 25 '17 at 10:57
  • i renamed the variable name also it's doesn't work to me. – Venkatesh Panabaka Jan 25 '17 at 11:00
  • i modified my question also. please check that – Venkatesh Panabaka Jan 25 '17 at 11:00
  • Now that you've read "Why does append return none in this code?" do you understand how to fix your code? To make the `print` function in your loop print everything on one line pass it an arg of `end=' '`. See the docs for further details. – PM 2Ring Jan 25 '17 at 11:12
  • "This question was marked as an *exact* duplicate of an existing question." I believe this is incorrect. If the `AttributeError` is solved, the question itself is still not solved and is way different than the one marked as duplicate @TigerhawkT3 @juanpa.arrivillaga – Markus Meskanen Jan 25 '17 at 11:16
  • It might be that the question should still be closed, but right now it's marked as a duplicate to a question which it is not a duplicate of. Solving the error provided in this post via the means provided in the duplicate post does not actually answer the question that was originally asked; "How to join multiple rows into single line using python?" – Markus Meskanen Jan 25 '17 at 11:24
  • I update my question what i am getting. still getting None value. It's append the value into list. How can i do this? – Venkatesh Panabaka Jan 25 '17 at 11:29
  • @VenkateshPanabaka See the question marked as duplicate, it should be enough to fix the `AttributeError` – Markus Meskanen Jan 25 '17 at 11:31
  • @TigerhawkT3 Please remove this question as duplicate. Please read the question before mark as duplicate. If your solve this problem it would be great. – Venkatesh Panabaka Jan 25 '17 at 11:31
  • @MarkusMeskanen No, done lot of research before raising a question. Problem still not solved. i updated my question please check that – Venkatesh Panabaka Jan 25 '17 at 11:34
  • @juanpa.arrivillaga Please remove this question as duplicate. Please read the question before mark as duplicate. If your solve this problem it would be great. – Venkatesh Panabaka Jan 25 '17 at 11:34
  • The `AttributeError` is directly explained in the other question (http://stackoverflow.com/questions/16641119/why-does-append-return-none-in-this-code) – Markus Meskanen Jan 25 '17 at 11:56

0 Answers0