5

In ruby I can do something like:

key, *rest = ["key1", 1, 2, 3]

and the result will be:

key = "key1"
rest = [1, 2, 3]

Is there a way to do the same in python?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
slebaron
  • 53
  • 2

1 Answers1

10
key,rest = my_list[0],my_list[1:]

is as close as you can get I think (in Python <= 2.7). In Python 3 your code works as is.

jamylak
  • 128,818
  • 30
  • 231
  • 230
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179