Given nested list element, find the one level back list value Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?How do I sort a list of dictionaries by a value of the dictionary?Finding the index of an item given a list containing it in PythonHow to return multiple values from a function?How do I remove an element from a list by index in Python?Find intersection of two nested lists?Getting the last element of a list in Python“Least Astonishment” and the Mutable Default ArgumentHow do I get the number of elements in a list in Python?Is there a simple way to delete a list element by value?Why is [] faster than list()?
Letter Boxed validator
3 doors, three guards, one stone
Is there a Spanish version of "dot your i's and cross your t's" that includes the letter 'ñ'?
Should I discuss the type of campaign with my players?
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?
Does surprise arrest existing movement?
Sorting numerically
Proof involving the spectral radius and Jordan Canonical form
How does a Death Domain cleric's Touch of Death feature work with Touch-range spells delivered by familiars?
If a contract sometimes uses the wrong name, is it still valid?
What do you call a plan that's an alternative plan in case your initial plan fails?
When to stop saving and start investing?
Is 1 ppb equal to 1 μg/kg?
Antler Helmet: Can it work?
How do I mention the quality of my school without bragging
How can I fade player when goes inside or outside of the area?
What is this single-engine low-wing propeller plane?
Disable hyphenation for an entire paragraph
Are my PIs rude or am I just being too sensitive?
Is it true that "carbohydrates are of no use for the basal metabolic need"?
Output the ŋarâþ crîþ alphabet song without using (m)any letters
Why does Python start at index -1 when indexing a list from the end?
If Jon Snow became King of the Seven Kingdoms what would his regnal number be?
Given nested list element, find the one level back list value
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?How do I sort a list of dictionaries by a value of the dictionary?Finding the index of an item given a list containing it in PythonHow to return multiple values from a function?How do I remove an element from a list by index in Python?Find intersection of two nested lists?Getting the last element of a list in Python“Least Astonishment” and the Mutable Default ArgumentHow do I get the number of elements in a list in Python?Is there a simple way to delete a list element by value?Why is [] faster than list()?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Say i have List=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
i know that if i call List[0][0]
i will get [['a', 'b'], ['c', 'd'], ['e', 'f']]
, and so on.
Is there any built-in or external python function(suppose its func(a)
) or way to get the nested list element a
one level back?
So if i call func(List[0][1])
those function will return List[0]
or when i call func(List[1][0][1])
those function will return List[1][0]
but if i call func(List)
it will return List
since it's already at the root. I've been searching for this kind of problem for hours but still couldn't find the solution.
python list indexing nested
|
show 1 more comment
Say i have List=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
i know that if i call List[0][0]
i will get [['a', 'b'], ['c', 'd'], ['e', 'f']]
, and so on.
Is there any built-in or external python function(suppose its func(a)
) or way to get the nested list element a
one level back?
So if i call func(List[0][1])
those function will return List[0]
or when i call func(List[1][0][1])
those function will return List[1][0]
but if i call func(List)
it will return List
since it's already at the root. I've been searching for this kind of problem for hours but still couldn't find the solution.
python list indexing nested
1
To summarize, if you see your list as a tree, you want a function that, given a subtree, returns the parent node of that subtree? If so, the answer is no (at least with list). There are, however, many tree implementations.
– Guybrush
Mar 22 at 8:25
yes exactly, i want to get back to the parent node of that list. So there really is not any alternative way to get this in python?
– Fauzi
Mar 22 at 8:28
1
The bottom line is, List is a not a bad structure to "build" that kind of logic/abstraction on, but a list simply ISNT itself an implementation of that kind of logic/structure. You would have to use a different structure or build your own. Once you understand that, you can start looking for better alternatives to "store/represent" this relation. Be it graphs, some type of dictionary/tree structure, or so on.
– Paritosh Singh
Mar 22 at 8:29
1
@Fauzi Indeed, lists are not suited to get the "parent", because lists weren't created with that specific use case in mind (obviously). That's why you need something "more". You can either go for trees, or implement your own structure (and both can be based on lists, of course).
– Guybrush
Mar 22 at 8:31
So that's how it is :( Thank you guys for pointing out that list can't be seen as parent-child like node logic.
– Fauzi
Mar 22 at 8:37
|
show 1 more comment
Say i have List=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
i know that if i call List[0][0]
i will get [['a', 'b'], ['c', 'd'], ['e', 'f']]
, and so on.
Is there any built-in or external python function(suppose its func(a)
) or way to get the nested list element a
one level back?
So if i call func(List[0][1])
those function will return List[0]
or when i call func(List[1][0][1])
those function will return List[1][0]
but if i call func(List)
it will return List
since it's already at the root. I've been searching for this kind of problem for hours but still couldn't find the solution.
python list indexing nested
Say i have List=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
i know that if i call List[0][0]
i will get [['a', 'b'], ['c', 'd'], ['e', 'f']]
, and so on.
Is there any built-in or external python function(suppose its func(a)
) or way to get the nested list element a
one level back?
So if i call func(List[0][1])
those function will return List[0]
or when i call func(List[1][0][1])
those function will return List[1][0]
but if i call func(List)
it will return List
since it's already at the root. I've been searching for this kind of problem for hours but still couldn't find the solution.
python list indexing nested
python list indexing nested
edited Mar 22 at 8:26
Fauzi
asked Mar 22 at 8:23
FauziFauzi
83
83
1
To summarize, if you see your list as a tree, you want a function that, given a subtree, returns the parent node of that subtree? If so, the answer is no (at least with list). There are, however, many tree implementations.
– Guybrush
Mar 22 at 8:25
yes exactly, i want to get back to the parent node of that list. So there really is not any alternative way to get this in python?
– Fauzi
Mar 22 at 8:28
1
The bottom line is, List is a not a bad structure to "build" that kind of logic/abstraction on, but a list simply ISNT itself an implementation of that kind of logic/structure. You would have to use a different structure or build your own. Once you understand that, you can start looking for better alternatives to "store/represent" this relation. Be it graphs, some type of dictionary/tree structure, or so on.
– Paritosh Singh
Mar 22 at 8:29
1
@Fauzi Indeed, lists are not suited to get the "parent", because lists weren't created with that specific use case in mind (obviously). That's why you need something "more". You can either go for trees, or implement your own structure (and both can be based on lists, of course).
– Guybrush
Mar 22 at 8:31
So that's how it is :( Thank you guys for pointing out that list can't be seen as parent-child like node logic.
– Fauzi
Mar 22 at 8:37
|
show 1 more comment
1
To summarize, if you see your list as a tree, you want a function that, given a subtree, returns the parent node of that subtree? If so, the answer is no (at least with list). There are, however, many tree implementations.
– Guybrush
Mar 22 at 8:25
yes exactly, i want to get back to the parent node of that list. So there really is not any alternative way to get this in python?
– Fauzi
Mar 22 at 8:28
1
The bottom line is, List is a not a bad structure to "build" that kind of logic/abstraction on, but a list simply ISNT itself an implementation of that kind of logic/structure. You would have to use a different structure or build your own. Once you understand that, you can start looking for better alternatives to "store/represent" this relation. Be it graphs, some type of dictionary/tree structure, or so on.
– Paritosh Singh
Mar 22 at 8:29
1
@Fauzi Indeed, lists are not suited to get the "parent", because lists weren't created with that specific use case in mind (obviously). That's why you need something "more". You can either go for trees, or implement your own structure (and both can be based on lists, of course).
– Guybrush
Mar 22 at 8:31
So that's how it is :( Thank you guys for pointing out that list can't be seen as parent-child like node logic.
– Fauzi
Mar 22 at 8:37
1
1
To summarize, if you see your list as a tree, you want a function that, given a subtree, returns the parent node of that subtree? If so, the answer is no (at least with list). There are, however, many tree implementations.
– Guybrush
Mar 22 at 8:25
To summarize, if you see your list as a tree, you want a function that, given a subtree, returns the parent node of that subtree? If so, the answer is no (at least with list). There are, however, many tree implementations.
– Guybrush
Mar 22 at 8:25
yes exactly, i want to get back to the parent node of that list. So there really is not any alternative way to get this in python?
– Fauzi
Mar 22 at 8:28
yes exactly, i want to get back to the parent node of that list. So there really is not any alternative way to get this in python?
– Fauzi
Mar 22 at 8:28
1
1
The bottom line is, List is a not a bad structure to "build" that kind of logic/abstraction on, but a list simply ISNT itself an implementation of that kind of logic/structure. You would have to use a different structure or build your own. Once you understand that, you can start looking for better alternatives to "store/represent" this relation. Be it graphs, some type of dictionary/tree structure, or so on.
– Paritosh Singh
Mar 22 at 8:29
The bottom line is, List is a not a bad structure to "build" that kind of logic/abstraction on, but a list simply ISNT itself an implementation of that kind of logic/structure. You would have to use a different structure or build your own. Once you understand that, you can start looking for better alternatives to "store/represent" this relation. Be it graphs, some type of dictionary/tree structure, or so on.
– Paritosh Singh
Mar 22 at 8:29
1
1
@Fauzi Indeed, lists are not suited to get the "parent", because lists weren't created with that specific use case in mind (obviously). That's why you need something "more". You can either go for trees, or implement your own structure (and both can be based on lists, of course).
– Guybrush
Mar 22 at 8:31
@Fauzi Indeed, lists are not suited to get the "parent", because lists weren't created with that specific use case in mind (obviously). That's why you need something "more". You can either go for trees, or implement your own structure (and both can be based on lists, of course).
– Guybrush
Mar 22 at 8:31
So that's how it is :( Thank you guys for pointing out that list can't be seen as parent-child like node logic.
– Fauzi
Mar 22 at 8:37
So that's how it is :( Thank you guys for pointing out that list can't be seen as parent-child like node logic.
– Fauzi
Mar 22 at 8:37
|
show 1 more comment
1 Answer
1
active
oldest
votes
You can use the following recursive function:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Testing it out:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Test Case 1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Test Case 2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Test Case 3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Test Case 4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None
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%2f55295525%2fgiven-nested-list-element-find-the-one-level-back-list-value%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 can use the following recursive function:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Testing it out:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Test Case 1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Test Case 2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Test Case 3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Test Case 4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None
add a comment |
You can use the following recursive function:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Testing it out:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Test Case 1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Test Case 2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Test Case 3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Test Case 4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None
add a comment |
You can use the following recursive function:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Testing it out:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Test Case 1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Test Case 2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Test Case 3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Test Case 4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None
You can use the following recursive function:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Testing it out:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Test Case 1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Test Case 2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Test Case 3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Test Case 4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None
answered Mar 22 at 9:40
fountainheadfountainhead
1,315313
1,315313
add a comment |
add a comment |
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%2f55295525%2fgiven-nested-list-element-find-the-one-level-back-list-value%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
1
To summarize, if you see your list as a tree, you want a function that, given a subtree, returns the parent node of that subtree? If so, the answer is no (at least with list). There are, however, many tree implementations.
– Guybrush
Mar 22 at 8:25
yes exactly, i want to get back to the parent node of that list. So there really is not any alternative way to get this in python?
– Fauzi
Mar 22 at 8:28
1
The bottom line is, List is a not a bad structure to "build" that kind of logic/abstraction on, but a list simply ISNT itself an implementation of that kind of logic/structure. You would have to use a different structure or build your own. Once you understand that, you can start looking for better alternatives to "store/represent" this relation. Be it graphs, some type of dictionary/tree structure, or so on.
– Paritosh Singh
Mar 22 at 8:29
1
@Fauzi Indeed, lists are not suited to get the "parent", because lists weren't created with that specific use case in mind (obviously). That's why you need something "more". You can either go for trees, or implement your own structure (and both can be based on lists, of course).
– Guybrush
Mar 22 at 8:31
So that's how it is :( Thank you guys for pointing out that list can't be seen as parent-child like node logic.
– Fauzi
Mar 22 at 8:37