Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass Tuple as a Slice
#1
I'd like to pass a tuple (2, 3) as a parameter value into a function (via array variable below), so that it replaces the slice value 2:3 in the third line. What would be the easiest way to achieve that?

def function(object, array, slices):
    selections = ... object ... 
    for i, selection in enumerate(selections[2:3]): # <= array variable replaces 2:3
        for x, y in slices:
            slur(object[x : y])

object = ...
tupleIndice = (2, 3)
tupleSlices = (0, 3), (3, 5)

function(staff, tupleIndice, tupleSlices)
My apology if this may not be the right place to ask this question. Thank you for your time and suggestion!
Reply
#2
To turn a tuple named x into a slice, use slice(*x).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 757 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  How to pass encrypted pass to pyodbc script tester_V 0 803 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Slice creates new objects? fmr300 4 1,268 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,286 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Slice list Moris526 1 1,618 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,055 Nov-10-2020, 11:35 PM
Last Post: KEYS
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,728 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Preferred way to slice a list SvetlanaofVodianova 3 2,484 Dec-09-2019, 11:50 PM
Last Post: SvetlanaofVodianova
  Pass by reference vs Pass by value leodavinci1990 1 2,165 Nov-20-2019, 02:05 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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