Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:35:49+00:00 2026-06-04T12:35:49+00:00

I have a list of tuples like a = [(1, 2), (2, 2), (3,

  • 0

I have a list of tuples like

a = [(1, 2), (2, 2), (3, 2)]

the length of this list will vary. The order may also be inverted eg:

a = [(3, 2), (2, 2), (1, 2)]

I also have a list of lists like:

b = [['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0']]

Now I would like to loop over this list of lists and modify items which have the indexes:

b[3][2]
b[2][2]
b[1][2]

So the indexes depend on a. I have tried this:

for item in indexes_list:
    b[item[0]][item[1]] = '1'

But this modifies the whole last column for me (I only want to modify the last 3 items of the last column with the indices given in a). What is the best way to accomplish this?

EDIT:
thanks for the responses but they have not worked for me. i have tried both and the same thing happens. i have also made that mistake on indexes_list (it was a typo) because i tried to simplify the problem. i should have elaborated further.

the list of lists is for a tic tac toe board of some sort. i have to test whether a move that is input by the user sandwiches an opponent piece. it doesn’t have to be a 3×3 board, therefore long moves are possible so that multiple opponent pieces can be sandwiched. if the move does sandwich an opponent piece, the opponent piece is obliterated, to be replaced by the user piece.

when i try both methods, it quickly does it for the whole column after the first time it goes over the for loop (by putting a breakpoint and debugging, this was detected). here is my prog’s code:

    for i, j in (indexes[0])[:len(y_p.rstrip('.'))]:
            print item
            board[i][j] = user
            print board

I used the print statement to see what happens while debugging. User is a variable that holds a str value of the user piece. The rstrip method is used to detect up to where the move should be.

By default, board is:

[['.', '.', '.', '.', '.'], ['.', 'X', 'O', '.', '.'], ['.', 'O', 'X', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]

y_p prints

'.OX.' # board[3][1] + board[2][1] + board[1][1] + board[0][1]

indexes[0] prints

[(3, 1), (2, 1), (1, 1), (0, 1)]

but i only want to do it for

[(3, 1), (2, 1), (1, 1)]

because the board[0][1] coordinate defines an empty space (invalid). The coordinate board[3][1] which is the first in this list is where the user wants to make his move. board[2][1] is an opponent piece and board[1][1] is a user piece. therefore it should turn the pieces defined by the coordinates

[(3, 1), (2, 1), (1, 1)]

to the user’s piece. But it does it for these coordinates instead (note that this is a 5×5 board)

[(4,1), (3, 1), (2, 1), (1, 1)]

so even board[4][1] gets modified even though its not in the initial indices list. After executing, board becomes:

[['.', 'X', '.', '.', '.'], ['.', 'X', 'O', '.', '.'], ['.', 'O', 'X', '.', '.'], ['.', 'X', '.', '.', '.'], ['.', 'X', '.', '.', '.']]

which isn’t right (the ‘X’ at the first and last rows shouldn’t be there)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:35:51+00:00Added an answer on June 4, 2026 at 12:35 pm

    One problem is that your list of indexes is called a but you refer to indexes_list.

    Fix this error and it works: (ideone)

    indexes_list = [(3, 2), (2, 2), (1, 2)]
    b = [['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0']]
    
    for item in indexes_list:
        b[item[0]][item[1]] = '1'
    
    print b
    

    Result:

    [['0', '0', '0'], ['0', '0', '1'], ['0', '0', '1'], ['0', '0', '1']]
    

    Note: before you say “But I tried it and it doesn’t work for me”, please read the code very carefully and note in particular how b is constructed. The following very similar alternatives do not work because they create a list containing 4 references to the same list.

    Wrong way to do it: (ideone)

    bb = ['0', '0', '0']
    b = [bb, bb, bb, bb]          # Wrong!!!
    

    Another wrong way to do it: (ideone)

    b = [['0', '0', '0']] * 4     # Wrong!!!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of tuples like this: [(username, 123), (df, 54), (as,2 34)]
I have a list of tuples that looks something like this: [('abc', 121),('abc', 231),('abc',
I have a list of lists like this: a = d[:3] >>> a [[90,
I have a list of tuples like this: [ ('a', 1), ('a', 2), ('a',
I have a list of tuples (always pairs) like this: [(0, 1), (2, 3),
I have a list of (label, count) tuples like this: [('grape', 100), ('grape', 3),
I have a list of tuples that look something like this: (Person 1,10) (Person
I have a list of tuples that looks like this: [('this', 'is'), ('is', 'the'),
I have a list of tuples that look like this. [ [(1,True),(2,True)] , [(3,False),(4,False),(5,False)]
I have a list of tuples in Python that I would like to output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.