Python Forum
Avoiding new user list sorting errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Avoiding new user list sorting errors
#1
It is very frustrating when a program runs to completion with no errors and gives unexpected output, when you thought you did everything correctly. In my case it was faulty syntax. I used my_list.sort instead of my_list.sort().

Luckily Pylint gave me a warning message that gave me a clue on how to fix the problem.

Sample code that demonstrates the problem:
my_list = ['bbb', 'ddd', 'aaa', 'ccc']

#NOTE: 'sep' argument defines the separator between print items
print()
print("Printing the original list\n", my_list, sep="")

#The 'mylist.sort' line generates the following Pylint WARNING: [W0104(pointless-statement), ] Statement seems to have no effect
print()
my_list.sort
print("Printing the list after 'mylist.sort'  (bad function call - does not sort)\n", my_list, sep="")

print()
my_list.sort()
print("Printing the list after 'mylist.sort()'\n", my_list, sep="")
Output:
Printing the original list ['bbb', 'ddd', 'aaa', 'ccc'] Printing the list after 'mylist.sort' (bad function call - does not sort) ['bbb', 'ddd', 'aaa', 'ccc'] Printing the list after 'mylist.sort()' ['aaa', 'bbb', 'ccc', 'ddd']
For more information about Pylint see:
https://pylint.readthedocs.io/en/latest/intro.html
https://pylint.readthedocs.io/en/latest/
https://pypi.python.org/pypi/pylint

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#2
Quote: It is very frustrating when a program runs to completion with no errors and gives unexpected output, when you thought you did everything correctly.
But if you don't get the output you expected, then, of course, you did something wrong!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  user friendly making a list and changing it hobbyprogrammer 2 2,976 Nov-10-2018, 03:14 PM
Last Post: hobbyprogrammer
  user defined list-based calculator mepyyeti 1 2,665 Mar-31-2018, 10:25 PM
Last Post: Almenon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020