0

Why this

print(tuplun[0:][1:])
tuplun = (["one", "two", "three"], ["hello", "scope"], ["pedro"])

results in this

(['hello', 'scope'], ['pedro'])    

and not in this?

(["two", "three"], ["hello", "scope"], ["pedro"])

Is there a way to achieve the last one?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 1
    You take slice `[0:]` of your tuple (which is the whole tuple) and from that you take slice `[1:]`, which omits the first element. So you get the whole tuple except without the first element. – khelwood Jul 04 '21 at 23:37
  • See [Understanding slice notation](https://stackoverflow.com/q/509211/3890632) – khelwood Jul 04 '21 at 23:39
  • `tuplun = tuplun[0][1:] + tuplin[1:]` – Tim Roberts Jul 04 '21 at 23:41
  • 1
    It seems that your requested result is inconsistent, all but first for the first sub-list, then all of the sub-list. – roadrunner66 Jul 04 '21 at 23:43
  • @timroberts Thanks for the answer! but I got this error: "can only concatenate list (not "tuple") to list " with your method – Christian Sama Jul 04 '21 at 23:49
  • @ChristianSama It would need to be `(tuplun[0],) + tuplun[1:]`, or `tuplun[:1] + tuplun[1:]`. Both of those produce the original `tuplun`. – Tom Karzes Jul 04 '21 at 23:56
  • Sorry. `tuplun = (tuplun[0][1:],) + tuplun[1:]`. The first needs to be turned in to a one-tuple. – Tim Roberts Jul 05 '21 at 00:10

2 Answers2

0

This should work

tuple([item[-2:] for item in tuplun])
dchoruzy
  • 249
  • 2
  • 8
-1

Firstly, note, getting each slice only operates on the outer tuple and that the lists inside are not sliced at all.

The first part of the expression [0:] means everything after and including the first element which means everything. This, therefore, does nothing. The next section [1:] means everything after and including the second element which means everything except the first element so ["one", "two", "three"] is thrown away.

tuplun = (["one", "two", "three"], ["hello", "scope"], ["pedro"])
tuplun2 = (tuplun[0][1:],)+tuplun[1:] # The comma is there to declare it is a tuple