Python Forum
how to refer to a subset of a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to refer to a subset of a list
#1
i have a function that is given a list and it stores a special value into the first position (index 0) of that list.
def store_special_value(where_to_store):
    ...
    if isinstance(where_to_store,list):
        where_to_store[0] = special_value
    return
i want to call that function and put that value into the n-th element a list:
def main(args)
    ...
    target = []
    ...
    temp_list = [None]
    store_special_value(temp_list)
    target[n] = temp_list[0]
    ...
my question is: is there a more direct way to do this without using the temporary list? that would mean a list reference that refers to a subset of an existing list (target) but operates as if it were a whole list. list slicing creates a new list instead of referring to an existing given list, so that is not a solution. is there a way to make a subset reference like that?

i do have a real use case for this, which is more complicated, but, i prefer to work with a minimal example case, to determine a more general use solution.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Do you mean like this?
special_value = 13
my_list = [1, 2, 3, 4, 5]
my_list.insert (2, special_value)
print (my_list)
Output:
[1, 2, 13, 3, 4, 5]
Gribouillis likes this post
Reply
#3
what i need is a way to use a function that will store into index 0 (that part cannot be changed) by giving it a subset reference. so if i want it to store into index 47 i would create a subset that starts at 47, such that accessing the subset at 0 accesses the original list at 47. subset[1] would be original[48]. this is done in pointer oriented languages by adding to the pointer. i don't know if it could be done in Python since i don't know the details of how it stores a list in memory. but there are ways to do it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
He wants a C pointer like thing.
int x[10];
int *y = &x[2];
Skaperen likes this post
Reply
#5
yes.

but i can envision a way to do it in an implementation of a list like object that contains values or references to other objects, with an interface API to this which can be done with methods.
    fake_list = real_list.subset(47,99)
    assert fake_list[0:52] == real_list[47:99]
i simply don't know if any implementation ever did this, like Python or Pike. i don't even want to look at Peril.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  bug in subset sum algorithm usercat123 3 1,387 Feb-07-2022, 05:41 AM
Last Post: deanhystad
  How to create subset in python? Bhavika 5 1,930 Nov-27-2021, 07:16 PM
Last Post: Gribouillis
  How to refer to particular cell GMCobraz 0 1,150 Jul-01-2020, 02:25 PM
Last Post: GMCobraz
  Grabbing a Subset of a String acemurdoc 3 2,603 Jun-18-2019, 04:57 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020