Creating a List - Creating one more level of list than wanted

Nore

I am able to successfully add to a list called donnes elements from a list called liste_dominos. However, it seems like I'm creating an extra level in the list.

While printing donnes, instead of having this output :

[[[5, 3], [6, 3], [2, 0], [4, 2], [2, 2], [6, 6]]], [[[2, 1], [6, 5], [6, 4], [3, 0], [3, 3], [3, 2]]]

I would get :

[[[[5, 3], [6, 3], [2, 0], [4, 2], [2, 2], [6, 6]]], [[[2, 1], [6, 5], [6, 4], [3, 0], [3, 3], [3, 2]]]]

Thus if I get the list of each player... instead of having this output : [[5, 3], [6, 3], [2, 0], [4, 2], [2, 2], [6, 6]]

I would get : [[[5, 3], [6, 3], [2, 0], [4, 2], [2, 2], [6, 6]]]

The list donnes should be formated as thus [[],[]]. Each element represents a player and in its sublist, the list of dominos they hold.

Here's the code.

import random

liste_dominos = [[6, 6], [6, 5], [6, 4], [6, 3], [6, 2], [6, 1], [6, 0], [5, 5], [5, 4], [5, 3], [5, 2], [5, 1], [5, 0], [4, 4], [4, 3], [4, 2], [4, 1], [4, 0], [3, 3], [3, 2], [3, 1], [3, 0], [2, 2], [2, 1], [2, 0], [1, 1], [1, 0], [0, 0]]
random.shuffle(liste_dominos)
no_players = 2
if int(no_players) == 2:
    donnes = [[] for i in range(int(no_players))]
    for x in range(no_players):
        donnes[x].append(liste_dominos[:6])
        del liste_dominos[:6]
    print(donnes[0])
    print(donnes[1])
    print(donnes)

The print is only there as an indication whether I get the correct result or not. I will be later on using the donnes list to perform other operations and thus need to get it in the aforementioned format.

Shailyn Ortiz

even tho there are better approaches, what is happening is that you are appending to the list what is returned from liste_dominos[:6] as it's, and this is basically a list of the elements contained in that range which are a list returning in fact [ [...],..,[...]]

Using extend to append the inner elements of the list solves this:

import random

liste_dominos = [[6, 6], [6, 5], [6, 4], [6, 3], [6, 2], [6, 1], [6, 0], [5, 5], [5, 4], [5, 3], [5, 2], [5, 1], [5, 0], [4, 4], [4, 3], [4, 2], [4, 1], [4, 0], [3, 3], [3, 2], [3, 1], [3, 0], [2, 2], [2, 1], [2, 0], [1, 1], [1, 0], [0, 0]]
random.shuffle(liste_dominos)
no_players = 2
if int(no_players) == 2:
    donnes = [[] for i in range(int(no_players))]
    for x in range(no_players):

        # Changed to extend instead of append
        # It can be changed to += also
        donnes[x].extend(liste_dominos[:6])
        del liste_dominos[:6]

    print(donnes[0])
    > [[1, 0], [6, 5], [4, 3], [5, 2], [6, 3], [5, 5]]

    print(donnes[1])
    > [[5, 1], [3, 2], [4, 1], [2, 1], [3, 0], [2, 2]]

    print(donnes)
    > [[[1, 0], [6, 5], [4, 3], [5, 2], [6, 3], [5, 5]], [[5, 1], [3, 2], [4, 1], [2, 1], [3, 0], [2, 2]]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Creating more than one class

C++ more than one instance of overloaded function matches the argument list when creating a header file

Creating a one query list

creating indexer for a class with more than one array?

error creating more than one semaphore

Creating more than one initial route

Creating more than one array for dataset

Creating more than one process using fork

One confusion when creating linked list with list

Creating Two Level List Comprehension in Python

Adding one more level of list using LINQ

How to stop click event from creating more than one element?

Laravel migrate creating table with more than one primary key

Creating more than one value for logged-in-timeout in Authlogic

Creating a proper channel for a single process generating more than one fasta

Lib(s)uinput: creating joystick with more than one button

@WebMvcTest creating more than one Controller for some reason

Creating single image by combining more than one images in iOS

Creating a subquery to retrieve data from more than one table

Creating a buttons within buttons with more than one text property

Creating new variable based on more than one condition

Creating an android Button which can perform more than one task

Creating more than one root type with generative type provider

Creating more than one row in a CSV file/Chess database creation

bluetoothctl creating gatt server - cant write more than one byte

Creating one list by combining two list in a list comprehension

Linked List creating a List

Python creating a list of list

Qt QJsonObjects - list deeper than one level?