Defining the first element of a list of lists in Python












1















I have a school assignment, and therefore we're required to use PyCharm and follow the PEP8 and Google standards regarding Python3. While my code works perfectly, PyCharm claims the index of rows[0] is of an unexpected value.



def create_matrix():
"""
Function made to create a matrix of who wins or who loses based on the number of figures in a game.
Keep in mind the order of the figures tuple. first element wins over second element, last element wins over
first element. an element also wins against the element two steps before it, in the case of five elements.
i haven't researched what relationships exist in higher dimensions, but there will always be an equal amount of
elements who beat you, as elements you beat. the missing element will be a draw.
"""
figures = ("Scissors", "Paper", "Rock", "Lizard", "Spock") # caution with order of elements
number_of_figures = len(figures) # expected to be an odd number
half_set = number_of_figures // 2 # half_set = int((n-1)/2)

win_and_lose = [+1, -1] # negative: row (player_b), positive: column (player_a), zero: draw
rows = [[None]*number_of_figures]*number_of_figures # uses same size in memory as a "full" set

rows[0] = [0] + win_and_lose*half_set # [0, -1, +1, -1, +1...]

for row in range(1, number_of_figures):
rows[row] = rotate(rows[row-1], 1)

# for row in range(0, len(rows)):
# print(rows[row])
return figures, rows


Linter warning



I have tested changing the specific line which is giving me a lint warning to



    rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4] = [0] + win_and_lose*half_set  # [0, -1, +1, -1, +1...]


and the code works without any lint warning. The problem I have is that I want the tuple to be expandable in size. I know I can do this with a for loop along the lines of



    for column in range(0, number_of_figures):
if column == 0:
rows[0][column] = 0
elif column % 2 == 0:
rows[0][column] = +1
elif column % 2 == 1:
rows[0][column] = -1
else:
raise Exception("Unexpected column value")


but I have some issues with this:


1) It isn't readable at all for someone who wants to look at the code.

2) When I try to print the list now, it has set all rows to [0, -1, +1, +1, -1], and not only the first element, as I expect it to do. This doesn't cause any runtime errors, but it's not what I want.



My questions is how I can write this piece code in a pythonic and easy to read manner, without breaking any Python standard.



Bonus question:
I want to minify the amount of memory usage of my code, and I've seen through __sizeof__() that if rows contains five elements with value None, it uses 80 bytes of memory. Same size is used if I have five elements with value [None, None, None, None, None]. The size remains unchanged once I set some values to the matrix. Any tips regarding this? I've set up the rotate() function so that I can rotate the list n positions at a time, so I could skip to any given row having the first row defined.










share|improve this question

























  • What's the linter warning?

    – Adam Smith
    Nov 26 '18 at 8:34






  • 2





    [[None]*number_of_figures]*number_of_figures does not create number_of_figures different rows. It creates a list of number_of_figures references to the same row. That is why changing one row changes all other rows, too.

    – DYZ
    Nov 26 '18 at 8:40






  • 1





    @AdamSmith I added a screenshot.

    – mazunki
    Nov 26 '18 at 8:43











  • Ah, I understand, @DYZ . Why does it work in the first case: rows[0] = [0] + win_and_lose*half_set ?

    – mazunki
    Nov 26 '18 at 8:45






  • 1





    You may find this useful.

    – DYZ
    Nov 26 '18 at 8:48
















1















I have a school assignment, and therefore we're required to use PyCharm and follow the PEP8 and Google standards regarding Python3. While my code works perfectly, PyCharm claims the index of rows[0] is of an unexpected value.



def create_matrix():
"""
Function made to create a matrix of who wins or who loses based on the number of figures in a game.
Keep in mind the order of the figures tuple. first element wins over second element, last element wins over
first element. an element also wins against the element two steps before it, in the case of five elements.
i haven't researched what relationships exist in higher dimensions, but there will always be an equal amount of
elements who beat you, as elements you beat. the missing element will be a draw.
"""
figures = ("Scissors", "Paper", "Rock", "Lizard", "Spock") # caution with order of elements
number_of_figures = len(figures) # expected to be an odd number
half_set = number_of_figures // 2 # half_set = int((n-1)/2)

win_and_lose = [+1, -1] # negative: row (player_b), positive: column (player_a), zero: draw
rows = [[None]*number_of_figures]*number_of_figures # uses same size in memory as a "full" set

rows[0] = [0] + win_and_lose*half_set # [0, -1, +1, -1, +1...]

for row in range(1, number_of_figures):
rows[row] = rotate(rows[row-1], 1)

# for row in range(0, len(rows)):
# print(rows[row])
return figures, rows


Linter warning



I have tested changing the specific line which is giving me a lint warning to



    rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4] = [0] + win_and_lose*half_set  # [0, -1, +1, -1, +1...]


and the code works without any lint warning. The problem I have is that I want the tuple to be expandable in size. I know I can do this with a for loop along the lines of



    for column in range(0, number_of_figures):
if column == 0:
rows[0][column] = 0
elif column % 2 == 0:
rows[0][column] = +1
elif column % 2 == 1:
rows[0][column] = -1
else:
raise Exception("Unexpected column value")


but I have some issues with this:


1) It isn't readable at all for someone who wants to look at the code.

2) When I try to print the list now, it has set all rows to [0, -1, +1, +1, -1], and not only the first element, as I expect it to do. This doesn't cause any runtime errors, but it's not what I want.



My questions is how I can write this piece code in a pythonic and easy to read manner, without breaking any Python standard.



Bonus question:
I want to minify the amount of memory usage of my code, and I've seen through __sizeof__() that if rows contains five elements with value None, it uses 80 bytes of memory. Same size is used if I have five elements with value [None, None, None, None, None]. The size remains unchanged once I set some values to the matrix. Any tips regarding this? I've set up the rotate() function so that I can rotate the list n positions at a time, so I could skip to any given row having the first row defined.










share|improve this question

























  • What's the linter warning?

    – Adam Smith
    Nov 26 '18 at 8:34






  • 2





    [[None]*number_of_figures]*number_of_figures does not create number_of_figures different rows. It creates a list of number_of_figures references to the same row. That is why changing one row changes all other rows, too.

    – DYZ
    Nov 26 '18 at 8:40






  • 1





    @AdamSmith I added a screenshot.

    – mazunki
    Nov 26 '18 at 8:43











  • Ah, I understand, @DYZ . Why does it work in the first case: rows[0] = [0] + win_and_lose*half_set ?

    – mazunki
    Nov 26 '18 at 8:45






  • 1





    You may find this useful.

    – DYZ
    Nov 26 '18 at 8:48














1












1








1








I have a school assignment, and therefore we're required to use PyCharm and follow the PEP8 and Google standards regarding Python3. While my code works perfectly, PyCharm claims the index of rows[0] is of an unexpected value.



def create_matrix():
"""
Function made to create a matrix of who wins or who loses based on the number of figures in a game.
Keep in mind the order of the figures tuple. first element wins over second element, last element wins over
first element. an element also wins against the element two steps before it, in the case of five elements.
i haven't researched what relationships exist in higher dimensions, but there will always be an equal amount of
elements who beat you, as elements you beat. the missing element will be a draw.
"""
figures = ("Scissors", "Paper", "Rock", "Lizard", "Spock") # caution with order of elements
number_of_figures = len(figures) # expected to be an odd number
half_set = number_of_figures // 2 # half_set = int((n-1)/2)

win_and_lose = [+1, -1] # negative: row (player_b), positive: column (player_a), zero: draw
rows = [[None]*number_of_figures]*number_of_figures # uses same size in memory as a "full" set

rows[0] = [0] + win_and_lose*half_set # [0, -1, +1, -1, +1...]

for row in range(1, number_of_figures):
rows[row] = rotate(rows[row-1], 1)

# for row in range(0, len(rows)):
# print(rows[row])
return figures, rows


Linter warning



I have tested changing the specific line which is giving me a lint warning to



    rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4] = [0] + win_and_lose*half_set  # [0, -1, +1, -1, +1...]


and the code works without any lint warning. The problem I have is that I want the tuple to be expandable in size. I know I can do this with a for loop along the lines of



    for column in range(0, number_of_figures):
if column == 0:
rows[0][column] = 0
elif column % 2 == 0:
rows[0][column] = +1
elif column % 2 == 1:
rows[0][column] = -1
else:
raise Exception("Unexpected column value")


but I have some issues with this:


1) It isn't readable at all for someone who wants to look at the code.

2) When I try to print the list now, it has set all rows to [0, -1, +1, +1, -1], and not only the first element, as I expect it to do. This doesn't cause any runtime errors, but it's not what I want.



My questions is how I can write this piece code in a pythonic and easy to read manner, without breaking any Python standard.



Bonus question:
I want to minify the amount of memory usage of my code, and I've seen through __sizeof__() that if rows contains five elements with value None, it uses 80 bytes of memory. Same size is used if I have five elements with value [None, None, None, None, None]. The size remains unchanged once I set some values to the matrix. Any tips regarding this? I've set up the rotate() function so that I can rotate the list n positions at a time, so I could skip to any given row having the first row defined.










share|improve this question
















I have a school assignment, and therefore we're required to use PyCharm and follow the PEP8 and Google standards regarding Python3. While my code works perfectly, PyCharm claims the index of rows[0] is of an unexpected value.



def create_matrix():
"""
Function made to create a matrix of who wins or who loses based on the number of figures in a game.
Keep in mind the order of the figures tuple. first element wins over second element, last element wins over
first element. an element also wins against the element two steps before it, in the case of five elements.
i haven't researched what relationships exist in higher dimensions, but there will always be an equal amount of
elements who beat you, as elements you beat. the missing element will be a draw.
"""
figures = ("Scissors", "Paper", "Rock", "Lizard", "Spock") # caution with order of elements
number_of_figures = len(figures) # expected to be an odd number
half_set = number_of_figures // 2 # half_set = int((n-1)/2)

win_and_lose = [+1, -1] # negative: row (player_b), positive: column (player_a), zero: draw
rows = [[None]*number_of_figures]*number_of_figures # uses same size in memory as a "full" set

rows[0] = [0] + win_and_lose*half_set # [0, -1, +1, -1, +1...]

for row in range(1, number_of_figures):
rows[row] = rotate(rows[row-1], 1)

# for row in range(0, len(rows)):
# print(rows[row])
return figures, rows


Linter warning



I have tested changing the specific line which is giving me a lint warning to



    rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4] = [0] + win_and_lose*half_set  # [0, -1, +1, -1, +1...]


and the code works without any lint warning. The problem I have is that I want the tuple to be expandable in size. I know I can do this with a for loop along the lines of



    for column in range(0, number_of_figures):
if column == 0:
rows[0][column] = 0
elif column % 2 == 0:
rows[0][column] = +1
elif column % 2 == 1:
rows[0][column] = -1
else:
raise Exception("Unexpected column value")


but I have some issues with this:


1) It isn't readable at all for someone who wants to look at the code.

2) When I try to print the list now, it has set all rows to [0, -1, +1, +1, -1], and not only the first element, as I expect it to do. This doesn't cause any runtime errors, but it's not what I want.



My questions is how I can write this piece code in a pythonic and easy to read manner, without breaking any Python standard.



Bonus question:
I want to minify the amount of memory usage of my code, and I've seen through __sizeof__() that if rows contains five elements with value None, it uses 80 bytes of memory. Same size is used if I have five elements with value [None, None, None, None, None]. The size remains unchanged once I set some values to the matrix. Any tips regarding this? I've set up the rotate() function so that I can rotate the list n positions at a time, so I could skip to any given row having the first row defined.







python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 8:41







mazunki

















asked Nov 26 '18 at 8:32









mazunkimazunki

1339




1339













  • What's the linter warning?

    – Adam Smith
    Nov 26 '18 at 8:34






  • 2





    [[None]*number_of_figures]*number_of_figures does not create number_of_figures different rows. It creates a list of number_of_figures references to the same row. That is why changing one row changes all other rows, too.

    – DYZ
    Nov 26 '18 at 8:40






  • 1





    @AdamSmith I added a screenshot.

    – mazunki
    Nov 26 '18 at 8:43











  • Ah, I understand, @DYZ . Why does it work in the first case: rows[0] = [0] + win_and_lose*half_set ?

    – mazunki
    Nov 26 '18 at 8:45






  • 1





    You may find this useful.

    – DYZ
    Nov 26 '18 at 8:48



















  • What's the linter warning?

    – Adam Smith
    Nov 26 '18 at 8:34






  • 2





    [[None]*number_of_figures]*number_of_figures does not create number_of_figures different rows. It creates a list of number_of_figures references to the same row. That is why changing one row changes all other rows, too.

    – DYZ
    Nov 26 '18 at 8:40






  • 1





    @AdamSmith I added a screenshot.

    – mazunki
    Nov 26 '18 at 8:43











  • Ah, I understand, @DYZ . Why does it work in the first case: rows[0] = [0] + win_and_lose*half_set ?

    – mazunki
    Nov 26 '18 at 8:45






  • 1





    You may find this useful.

    – DYZ
    Nov 26 '18 at 8:48

















What's the linter warning?

– Adam Smith
Nov 26 '18 at 8:34





What's the linter warning?

– Adam Smith
Nov 26 '18 at 8:34




2




2





[[None]*number_of_figures]*number_of_figures does not create number_of_figures different rows. It creates a list of number_of_figures references to the same row. That is why changing one row changes all other rows, too.

– DYZ
Nov 26 '18 at 8:40





[[None]*number_of_figures]*number_of_figures does not create number_of_figures different rows. It creates a list of number_of_figures references to the same row. That is why changing one row changes all other rows, too.

– DYZ
Nov 26 '18 at 8:40




1




1





@AdamSmith I added a screenshot.

– mazunki
Nov 26 '18 at 8:43





@AdamSmith I added a screenshot.

– mazunki
Nov 26 '18 at 8:43













Ah, I understand, @DYZ . Why does it work in the first case: rows[0] = [0] + win_and_lose*half_set ?

– mazunki
Nov 26 '18 at 8:45





Ah, I understand, @DYZ . Why does it work in the first case: rows[0] = [0] + win_and_lose*half_set ?

– mazunki
Nov 26 '18 at 8:45




1




1





You may find this useful.

– DYZ
Nov 26 '18 at 8:48





You may find this useful.

– DYZ
Nov 26 '18 at 8:48












2 Answers
2






active

oldest

votes


















0














Assuming the comment by DYZ above isn't existent in the first solution, you can use type hinting to help the linter understand the type of rows. In my code this eliminates the linter warning.



rows = [[None]*number_of_figures]*number_of_figures  # type: list #uses same size in memory as a "full" set


This syntax is also available for python 3:



rows:list = [[None]*number_of_figures]*number_of_figures





share|improve this answer





















  • 1





    or rows: List[int] = ... if you do from typing import List.

    – Adam Smith
    Nov 26 '18 at 8:48











  • Didn't know about type hinting to the linter, but it didn't fix my linter warning. The linter is at index of rows[0] two lines below. Do I need to change some setting for it to work?

    – mazunki
    Nov 26 '18 at 8:50











  • Which python version?

    – Shushan
    Nov 26 '18 at 8:51











  • Python v3.6 @Shushan

    – mazunki
    Nov 26 '18 at 8:53






  • 1





    rows: list did the trick, even with the casting issue @AdamSmith mentioned. It complained about a missing space, though. :)

    – mazunki
    Nov 26 '18 at 8:59



















2














PyCharm is complaining because you initialize rows as a list of list of Nones, then re-assign it to be a list of list of ints. Regardless it's a linter warning you are safe to ignore, since dynamic typing is something Python is supposed to do (if you want static typing, go write in a static language).



You could fix it by simply initializing as:



rows = [[0 for _ in range(number_of_figures)] for _ in range(number_of_figures)]
# this is identical to `[[0] * number_of_figures] * number_of_figures` except it
# avoids the duplication of references issue you were experiencing that DYZ
# pointed out in the comments above.





share|improve this answer
























  • Oh, this fixed the linter warning. I understand why it complained now, but it wouldn't be runtime-safe to set it to zero, as that's my default draw situation. I can change this to some other number, but I'd prefer to avoid it. Can I somehow tell it to ignore the type-casting from None-type to Integer?

    – mazunki
    Nov 26 '18 at 8:56






  • 1





    @mazunki it's safe to ignore the linter warning entirely. Linters can complain -- that's allowed -- sometimes lint doesn't indicate bad code.

    – Adam Smith
    Nov 26 '18 at 8:57






  • 1





    @mazunki also: you should look at collections.deque. It's a cute way to sidestep this whole problem.

    – Adam Smith
    Nov 26 '18 at 8:58











  • Personally I only care about code working too, but school has all sorts of requirements for grading, I guess. It's a nice tip and reminder, anyway. I'll take a look at collection deques.

    – mazunki
    Nov 26 '18 at 9:10






  • 1





    @mazunki see this sample code

    – Adam Smith
    Nov 26 '18 at 9:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477237%2fdefining-the-first-element-of-a-list-of-lists-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Assuming the comment by DYZ above isn't existent in the first solution, you can use type hinting to help the linter understand the type of rows. In my code this eliminates the linter warning.



rows = [[None]*number_of_figures]*number_of_figures  # type: list #uses same size in memory as a "full" set


This syntax is also available for python 3:



rows:list = [[None]*number_of_figures]*number_of_figures





share|improve this answer





















  • 1





    or rows: List[int] = ... if you do from typing import List.

    – Adam Smith
    Nov 26 '18 at 8:48











  • Didn't know about type hinting to the linter, but it didn't fix my linter warning. The linter is at index of rows[0] two lines below. Do I need to change some setting for it to work?

    – mazunki
    Nov 26 '18 at 8:50











  • Which python version?

    – Shushan
    Nov 26 '18 at 8:51











  • Python v3.6 @Shushan

    – mazunki
    Nov 26 '18 at 8:53






  • 1





    rows: list did the trick, even with the casting issue @AdamSmith mentioned. It complained about a missing space, though. :)

    – mazunki
    Nov 26 '18 at 8:59
















0














Assuming the comment by DYZ above isn't existent in the first solution, you can use type hinting to help the linter understand the type of rows. In my code this eliminates the linter warning.



rows = [[None]*number_of_figures]*number_of_figures  # type: list #uses same size in memory as a "full" set


This syntax is also available for python 3:



rows:list = [[None]*number_of_figures]*number_of_figures





share|improve this answer





















  • 1





    or rows: List[int] = ... if you do from typing import List.

    – Adam Smith
    Nov 26 '18 at 8:48











  • Didn't know about type hinting to the linter, but it didn't fix my linter warning. The linter is at index of rows[0] two lines below. Do I need to change some setting for it to work?

    – mazunki
    Nov 26 '18 at 8:50











  • Which python version?

    – Shushan
    Nov 26 '18 at 8:51











  • Python v3.6 @Shushan

    – mazunki
    Nov 26 '18 at 8:53






  • 1





    rows: list did the trick, even with the casting issue @AdamSmith mentioned. It complained about a missing space, though. :)

    – mazunki
    Nov 26 '18 at 8:59














0












0








0







Assuming the comment by DYZ above isn't existent in the first solution, you can use type hinting to help the linter understand the type of rows. In my code this eliminates the linter warning.



rows = [[None]*number_of_figures]*number_of_figures  # type: list #uses same size in memory as a "full" set


This syntax is also available for python 3:



rows:list = [[None]*number_of_figures]*number_of_figures





share|improve this answer















Assuming the comment by DYZ above isn't existent in the first solution, you can use type hinting to help the linter understand the type of rows. In my code this eliminates the linter warning.



rows = [[None]*number_of_figures]*number_of_figures  # type: list #uses same size in memory as a "full" set


This syntax is also available for python 3:



rows:list = [[None]*number_of_figures]*number_of_figures






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 8:56

























answered Nov 26 '18 at 8:46









ShushanShushan

1,1171915




1,1171915








  • 1





    or rows: List[int] = ... if you do from typing import List.

    – Adam Smith
    Nov 26 '18 at 8:48











  • Didn't know about type hinting to the linter, but it didn't fix my linter warning. The linter is at index of rows[0] two lines below. Do I need to change some setting for it to work?

    – mazunki
    Nov 26 '18 at 8:50











  • Which python version?

    – Shushan
    Nov 26 '18 at 8:51











  • Python v3.6 @Shushan

    – mazunki
    Nov 26 '18 at 8:53






  • 1





    rows: list did the trick, even with the casting issue @AdamSmith mentioned. It complained about a missing space, though. :)

    – mazunki
    Nov 26 '18 at 8:59














  • 1





    or rows: List[int] = ... if you do from typing import List.

    – Adam Smith
    Nov 26 '18 at 8:48











  • Didn't know about type hinting to the linter, but it didn't fix my linter warning. The linter is at index of rows[0] two lines below. Do I need to change some setting for it to work?

    – mazunki
    Nov 26 '18 at 8:50











  • Which python version?

    – Shushan
    Nov 26 '18 at 8:51











  • Python v3.6 @Shushan

    – mazunki
    Nov 26 '18 at 8:53






  • 1





    rows: list did the trick, even with the casting issue @AdamSmith mentioned. It complained about a missing space, though. :)

    – mazunki
    Nov 26 '18 at 8:59








1




1





or rows: List[int] = ... if you do from typing import List.

– Adam Smith
Nov 26 '18 at 8:48





or rows: List[int] = ... if you do from typing import List.

– Adam Smith
Nov 26 '18 at 8:48













Didn't know about type hinting to the linter, but it didn't fix my linter warning. The linter is at index of rows[0] two lines below. Do I need to change some setting for it to work?

– mazunki
Nov 26 '18 at 8:50





Didn't know about type hinting to the linter, but it didn't fix my linter warning. The linter is at index of rows[0] two lines below. Do I need to change some setting for it to work?

– mazunki
Nov 26 '18 at 8:50













Which python version?

– Shushan
Nov 26 '18 at 8:51





Which python version?

– Shushan
Nov 26 '18 at 8:51













Python v3.6 @Shushan

– mazunki
Nov 26 '18 at 8:53





Python v3.6 @Shushan

– mazunki
Nov 26 '18 at 8:53




1




1





rows: list did the trick, even with the casting issue @AdamSmith mentioned. It complained about a missing space, though. :)

– mazunki
Nov 26 '18 at 8:59





rows: list did the trick, even with the casting issue @AdamSmith mentioned. It complained about a missing space, though. :)

– mazunki
Nov 26 '18 at 8:59













2














PyCharm is complaining because you initialize rows as a list of list of Nones, then re-assign it to be a list of list of ints. Regardless it's a linter warning you are safe to ignore, since dynamic typing is something Python is supposed to do (if you want static typing, go write in a static language).



You could fix it by simply initializing as:



rows = [[0 for _ in range(number_of_figures)] for _ in range(number_of_figures)]
# this is identical to `[[0] * number_of_figures] * number_of_figures` except it
# avoids the duplication of references issue you were experiencing that DYZ
# pointed out in the comments above.





share|improve this answer
























  • Oh, this fixed the linter warning. I understand why it complained now, but it wouldn't be runtime-safe to set it to zero, as that's my default draw situation. I can change this to some other number, but I'd prefer to avoid it. Can I somehow tell it to ignore the type-casting from None-type to Integer?

    – mazunki
    Nov 26 '18 at 8:56






  • 1





    @mazunki it's safe to ignore the linter warning entirely. Linters can complain -- that's allowed -- sometimes lint doesn't indicate bad code.

    – Adam Smith
    Nov 26 '18 at 8:57






  • 1





    @mazunki also: you should look at collections.deque. It's a cute way to sidestep this whole problem.

    – Adam Smith
    Nov 26 '18 at 8:58











  • Personally I only care about code working too, but school has all sorts of requirements for grading, I guess. It's a nice tip and reminder, anyway. I'll take a look at collection deques.

    – mazunki
    Nov 26 '18 at 9:10






  • 1





    @mazunki see this sample code

    – Adam Smith
    Nov 26 '18 at 9:14
















2














PyCharm is complaining because you initialize rows as a list of list of Nones, then re-assign it to be a list of list of ints. Regardless it's a linter warning you are safe to ignore, since dynamic typing is something Python is supposed to do (if you want static typing, go write in a static language).



You could fix it by simply initializing as:



rows = [[0 for _ in range(number_of_figures)] for _ in range(number_of_figures)]
# this is identical to `[[0] * number_of_figures] * number_of_figures` except it
# avoids the duplication of references issue you were experiencing that DYZ
# pointed out in the comments above.





share|improve this answer
























  • Oh, this fixed the linter warning. I understand why it complained now, but it wouldn't be runtime-safe to set it to zero, as that's my default draw situation. I can change this to some other number, but I'd prefer to avoid it. Can I somehow tell it to ignore the type-casting from None-type to Integer?

    – mazunki
    Nov 26 '18 at 8:56






  • 1





    @mazunki it's safe to ignore the linter warning entirely. Linters can complain -- that's allowed -- sometimes lint doesn't indicate bad code.

    – Adam Smith
    Nov 26 '18 at 8:57






  • 1





    @mazunki also: you should look at collections.deque. It's a cute way to sidestep this whole problem.

    – Adam Smith
    Nov 26 '18 at 8:58











  • Personally I only care about code working too, but school has all sorts of requirements for grading, I guess. It's a nice tip and reminder, anyway. I'll take a look at collection deques.

    – mazunki
    Nov 26 '18 at 9:10






  • 1





    @mazunki see this sample code

    – Adam Smith
    Nov 26 '18 at 9:14














2












2








2







PyCharm is complaining because you initialize rows as a list of list of Nones, then re-assign it to be a list of list of ints. Regardless it's a linter warning you are safe to ignore, since dynamic typing is something Python is supposed to do (if you want static typing, go write in a static language).



You could fix it by simply initializing as:



rows = [[0 for _ in range(number_of_figures)] for _ in range(number_of_figures)]
# this is identical to `[[0] * number_of_figures] * number_of_figures` except it
# avoids the duplication of references issue you were experiencing that DYZ
# pointed out in the comments above.





share|improve this answer













PyCharm is complaining because you initialize rows as a list of list of Nones, then re-assign it to be a list of list of ints. Regardless it's a linter warning you are safe to ignore, since dynamic typing is something Python is supposed to do (if you want static typing, go write in a static language).



You could fix it by simply initializing as:



rows = [[0 for _ in range(number_of_figures)] for _ in range(number_of_figures)]
# this is identical to `[[0] * number_of_figures] * number_of_figures` except it
# avoids the duplication of references issue you were experiencing that DYZ
# pointed out in the comments above.






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 8:52









Adam SmithAdam Smith

35.1k73376




35.1k73376













  • Oh, this fixed the linter warning. I understand why it complained now, but it wouldn't be runtime-safe to set it to zero, as that's my default draw situation. I can change this to some other number, but I'd prefer to avoid it. Can I somehow tell it to ignore the type-casting from None-type to Integer?

    – mazunki
    Nov 26 '18 at 8:56






  • 1





    @mazunki it's safe to ignore the linter warning entirely. Linters can complain -- that's allowed -- sometimes lint doesn't indicate bad code.

    – Adam Smith
    Nov 26 '18 at 8:57






  • 1





    @mazunki also: you should look at collections.deque. It's a cute way to sidestep this whole problem.

    – Adam Smith
    Nov 26 '18 at 8:58











  • Personally I only care about code working too, but school has all sorts of requirements for grading, I guess. It's a nice tip and reminder, anyway. I'll take a look at collection deques.

    – mazunki
    Nov 26 '18 at 9:10






  • 1





    @mazunki see this sample code

    – Adam Smith
    Nov 26 '18 at 9:14



















  • Oh, this fixed the linter warning. I understand why it complained now, but it wouldn't be runtime-safe to set it to zero, as that's my default draw situation. I can change this to some other number, but I'd prefer to avoid it. Can I somehow tell it to ignore the type-casting from None-type to Integer?

    – mazunki
    Nov 26 '18 at 8:56






  • 1





    @mazunki it's safe to ignore the linter warning entirely. Linters can complain -- that's allowed -- sometimes lint doesn't indicate bad code.

    – Adam Smith
    Nov 26 '18 at 8:57






  • 1





    @mazunki also: you should look at collections.deque. It's a cute way to sidestep this whole problem.

    – Adam Smith
    Nov 26 '18 at 8:58











  • Personally I only care about code working too, but school has all sorts of requirements for grading, I guess. It's a nice tip and reminder, anyway. I'll take a look at collection deques.

    – mazunki
    Nov 26 '18 at 9:10






  • 1





    @mazunki see this sample code

    – Adam Smith
    Nov 26 '18 at 9:14

















Oh, this fixed the linter warning. I understand why it complained now, but it wouldn't be runtime-safe to set it to zero, as that's my default draw situation. I can change this to some other number, but I'd prefer to avoid it. Can I somehow tell it to ignore the type-casting from None-type to Integer?

– mazunki
Nov 26 '18 at 8:56





Oh, this fixed the linter warning. I understand why it complained now, but it wouldn't be runtime-safe to set it to zero, as that's my default draw situation. I can change this to some other number, but I'd prefer to avoid it. Can I somehow tell it to ignore the type-casting from None-type to Integer?

– mazunki
Nov 26 '18 at 8:56




1




1





@mazunki it's safe to ignore the linter warning entirely. Linters can complain -- that's allowed -- sometimes lint doesn't indicate bad code.

– Adam Smith
Nov 26 '18 at 8:57





@mazunki it's safe to ignore the linter warning entirely. Linters can complain -- that's allowed -- sometimes lint doesn't indicate bad code.

– Adam Smith
Nov 26 '18 at 8:57




1




1





@mazunki also: you should look at collections.deque. It's a cute way to sidestep this whole problem.

– Adam Smith
Nov 26 '18 at 8:58





@mazunki also: you should look at collections.deque. It's a cute way to sidestep this whole problem.

– Adam Smith
Nov 26 '18 at 8:58













Personally I only care about code working too, but school has all sorts of requirements for grading, I guess. It's a nice tip and reminder, anyway. I'll take a look at collection deques.

– mazunki
Nov 26 '18 at 9:10





Personally I only care about code working too, but school has all sorts of requirements for grading, I guess. It's a nice tip and reminder, anyway. I'll take a look at collection deques.

– mazunki
Nov 26 '18 at 9:10




1




1





@mazunki see this sample code

– Adam Smith
Nov 26 '18 at 9:14





@mazunki see this sample code

– Adam Smith
Nov 26 '18 at 9:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477237%2fdefining-the-first-element-of-a-list-of-lists-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Error adding annotation colours to pheatmap in R: “more elements supplied than there are to replace”

15 giugno