Unpack 2d list of tuplesHow do I check if a list is empty?Hidden features of PythonFinding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?Convert bytes to a string?What's the difference between lists and tuples?How to make a flat list out of list of listsHow to clone or copy a list?How to sort (list/tuple) of lists/tuples by the element at a given index?How do I list all files of a directory?
How do I check that users don't write down their passwords?
What is it called when the tritone is added to a minor scale?
Do intermediate subdomains need to exist?
Is there a standard definition of the "stall" phenomena?
Why do Klingons use cloaking devices?
Should I cheat if the majority does it?
Way to see all encrypted fields in Salesforce?
Check whether quotient ring is a field or an integral domain
Convert integer to full text string duration
Stay in US on J-1 visa after quitting job?
What is exact meaning of “ich wäre gern”?
Should I increase my 401(k) contributions, or increase my mortgage payments
Is it possible to spoof an IP address to an exact number?
Convert Front Door Entry Mailbox to a Front & Rear Door Entry Mailbox
Sleepy tired vs physically tired
Why do we need a bootloader separate from our application program in microcontrollers?
Taking my Ph.D. advisor out for dinner after graduation
What is the highest level of accuracy in motion control a Victorian society could achieve?
Is conquering your neighbors to fight a greater enemy a valid strategy?
Can you take the Dodge action while prone?
How frequently do Russian people still refer to others by their patronymic (отчество)?
Why did Super-VGA offer the 5:4 1280*1024 resolution?
Do Goblin tokens count as Goblins?
Does 5e have an equivalent of the Psychic Paper from Doctor Who?
Unpack 2d list of tuples
How do I check if a list is empty?Hidden features of PythonFinding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?Convert bytes to a string?What's the difference between lists and tuples?How to make a flat list out of list of listsHow to clone or copy a list?How to sort (list/tuple) of lists/tuples by the element at a given index?How do I list all files of a directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to unpack a 2D list of tuples. Everything I have tried so far is not giving me the results
This is what I am trying to achieve:
Input: [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
Output = [[-8, 2], [0, -1]], [[-8, 0], [2, -1]]
I have tried zip like this but it does not work.
a,b = zip(*matrixRow)
python list tuples
add a comment |
I am trying to unpack a 2D list of tuples. Everything I have tried so far is not giving me the results
This is what I am trying to achieve:
Input: [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
Output = [[-8, 2], [0, -1]], [[-8, 0], [2, -1]]
I have tried zip like this but it does not work.
a,b = zip(*matrixRow)
python list tuples
add a comment |
I am trying to unpack a 2D list of tuples. Everything I have tried so far is not giving me the results
This is what I am trying to achieve:
Input: [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
Output = [[-8, 2], [0, -1]], [[-8, 0], [2, -1]]
I have tried zip like this but it does not work.
a,b = zip(*matrixRow)
python list tuples
I am trying to unpack a 2D list of tuples. Everything I have tried so far is not giving me the results
This is what I am trying to achieve:
Input: [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
Output = [[-8, 2], [0, -1]], [[-8, 0], [2, -1]]
I have tried zip like this but it does not work.
a,b = zip(*matrixRow)
python list tuples
python list tuples
edited Mar 25 at 20:03
Pavan Chandaka
5,7323 gold badges11 silver badges20 bronze badges
5,7323 gold badges11 silver badges20 bronze badges
asked Mar 25 at 20:01
Firzok NadeemFirzok Nadeem
83 bronze badges
83 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
you need to apply zip
twice to perform 2 transpositions:
inlist = [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
print(list(zip(*(zip(*x) for x in inlist))))
result:
[((-8, 2), (0, -1)), ((-8, 0), (2, -1))]
this gives tuples
inside because zip
creates them. To get lists, it's a little more complicated because you have to convert to list
at 2 levels:
print([[list(z) for z in y] for y in zip(*(zip(*x) for x in inlist))])
prints:
[[[-8, 2], [0, -1]], [[-8, 0], [2, -1]]]
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345558%2funpack-2d-list-of-tuples%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you need to apply zip
twice to perform 2 transpositions:
inlist = [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
print(list(zip(*(zip(*x) for x in inlist))))
result:
[((-8, 2), (0, -1)), ((-8, 0), (2, -1))]
this gives tuples
inside because zip
creates them. To get lists, it's a little more complicated because you have to convert to list
at 2 levels:
print([[list(z) for z in y] for y in zip(*(zip(*x) for x in inlist))])
prints:
[[[-8, 2], [0, -1]], [[-8, 0], [2, -1]]]
add a comment |
you need to apply zip
twice to perform 2 transpositions:
inlist = [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
print(list(zip(*(zip(*x) for x in inlist))))
result:
[((-8, 2), (0, -1)), ((-8, 0), (2, -1))]
this gives tuples
inside because zip
creates them. To get lists, it's a little more complicated because you have to convert to list
at 2 levels:
print([[list(z) for z in y] for y in zip(*(zip(*x) for x in inlist))])
prints:
[[[-8, 2], [0, -1]], [[-8, 0], [2, -1]]]
add a comment |
you need to apply zip
twice to perform 2 transpositions:
inlist = [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
print(list(zip(*(zip(*x) for x in inlist))))
result:
[((-8, 2), (0, -1)), ((-8, 0), (2, -1))]
this gives tuples
inside because zip
creates them. To get lists, it's a little more complicated because you have to convert to list
at 2 levels:
print([[list(z) for z in y] for y in zip(*(zip(*x) for x in inlist))])
prints:
[[[-8, 2], [0, -1]], [[-8, 0], [2, -1]]]
you need to apply zip
twice to perform 2 transpositions:
inlist = [[(-8, -8), (2, 0)], [(0, 2), (-1, -1)]]
print(list(zip(*(zip(*x) for x in inlist))))
result:
[((-8, 2), (0, -1)), ((-8, 0), (2, -1))]
this gives tuples
inside because zip
creates them. To get lists, it's a little more complicated because you have to convert to list
at 2 levels:
print([[list(z) for z in y] for y in zip(*(zip(*x) for x in inlist))])
prints:
[[[-8, 2], [0, -1]], [[-8, 0], [2, -1]]]
edited Mar 25 at 20:16
answered Mar 25 at 20:08
Jean-François Fabre♦Jean-François Fabre
109k10 gold badges68 silver badges123 bronze badges
109k10 gold badges68 silver badges123 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345558%2funpack-2d-list-of-tuples%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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