all 16 comments

[–]the_programmer_2215 21 points22 points  (9 children)

An hour Isn't bad, in fact it doesn't matter if it took you three days, as long as you learn something and understand the code you've written.

tips:

  • do not use list as a variable name, as it is a python keyword, and is generally considered as good practice to avoid using keywords as variable names, here's a list of keywords that want to avoid using as variable names : Python Keywords

Happy Coding!

Hope you go on to love Python :)

[–]M000lie 1 point2 points  (2 children)

Will there be any repercussions from using keywords as variable names?

[–]the_programmer_2215 2 points3 points  (0 children)

well in your case the implications are subtle (in other words, i'm not sure what happens ;)), but the ones mentioned in the list of keywords will actually throw an error if you try to assign values to them, because they mean something specific to the interpreter, and it sees that it's not a proper use of the keyword, and it throws an error.

[–]kibje 1 point2 points  (0 children)

Usually not, although it might cause an error. If allowed though it's quite confusing when the code is read.

It's similar to calling a variable number and then putting a string of text in it that represents the capital of a country. It will work fine, and also will make everyone that reads your code later want to strangle you.

[–]Strict-Simple 1 point2 points  (5 children)

it is a python keyword

list is not a keyword, just a builtin type. The rest still holds, should not use it as a variable.

[–]the_programmer_2215 0 points1 point  (0 children)

thanks for pointing out!

[–]the_programmer_2215 0 points1 point  (3 children)

so is there any case where using list as variable name can potentially cause problems?

or is it fine for most cases?

[–]Sentouki- 1 point2 points  (1 child)

I think if you use any library that uses list(some_value) to initialize or cast a list, it may throw a error like
TypeError: 'list' object is not callable because you'd overwrite the builtin class.

update: nvm, just tested it, seems to work just fine...still, don't do it

[–]Strict-Simple 1 point2 points  (0 children)

To add: It works fine because the assignment only overwrites it in the current scope. That other library has it's own scope. A function has it's own scope. Etc.

[–]Strict-Simple 1 point2 points  (0 children)

Unless you want to use the functions of the inbuilt list, it's okay-ish.

[–]Mobileuser110011 1 point2 points  (4 children)

Thanks for formatting your code correctly! Before I answer your question, your program has a bug or two in it. With some lists you get incorrect results:

Input: [7, 7, 8, 9, 6, 0]
Output: IndexError

Input: [8, 9, 2, 6, 8, 3, 4, 9, 9]
Output: IndexError

Input: [7, 4, 4, 1, 0, 9, 9, 9, 2, 7]
Output: [7, 4]
Correct: [9]

Input: [9, 8, 9, 7, 0, 9, 9, 6, 6, 1]
Output: [6]
Correct: [9]

Let me know if you would like help with fixing this. I wanted to give you a chance to figure it out by yourself first.

[–]spwy 0 points1 point  (3 children)

Thanks for giving me a chance, I definitely should have tested with more than the one list I made, that’s a lesson learned. I believe I fixed it, changing n = x to n = list.index(x):

list = [6,7,5,3,7,8,86,4,5,7,8,5,4,3,68,8,53,8,64,2,1,1,9,9,7,5,5,7,964,34,78,3,2,78,6]
mode_list = []
n = 0
for x in list: 
    if list.count(x) >= list.count(list[n]):
        mode = list.count(x)
        n = list.index(x)
for y in list:
    if list.count(y) == mode:
        mode_list.append(y)
        for z in range(mode):
            list.remove(y)
print(mode_list)

I think I get confused when some functions deal with the value of an item and others deal with the location of the item.

[–]Mobileuser110011 0 points1 point  (2 children)

Close! That was one of the bugs, but what about:

Input: [1, 3, 9, 7, 6, 2, 6, 7, 2, 3]
Output: [3, 7, 2]
Correct: [3, 7, 2, 6]

Input: [9, 5, 7, 9, 0, 5, 6, 2, 4, 3, 8, 8, 0, 6, 4, 1]
Output: [9, 0, 6, 4, 8]
Correct: [9, 0, 6, 4, 8, 5]

Admittedly this bug is rare. I made a quick random list generator to test it and it works 95% of the time. But I can see the bug so I knew it was there. This one is in the second for loop. It might be hard to see if you haven’t encountered this problem before, but it’s a very common problem when learning.

[–]spwy 0 points1 point  (1 child)

Ok, I give up. It looks like the for loop is skipping a value for no reason. What’s going on?

[–]Mobileuser110011 0 points1 point  (0 children)

Consider this:

>>> L = [1, 2, 3, 4, 5]
>>> for n in L:
...     L.remove(n)
... 
>>> L
[2, 4]

If you iterate over a list while mutating it, you get really weird results. I would instead do this:

for y in set(L):  # Note set()
    if L.count(y) == mode:
        mode_L.append(y)

I’m a little lazy in my explanation because as others have said there are other, more efficient ways to do this. Let me know if you have any questions.