3

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
  • 110,383
  • 23
  • 155
  • 178
slebaron
  • 33
  • 2

1 Answers1

8
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
  • 120,885
  • 29
  • 225
  • 225
Joran Beasley
  • 103,130
  • 11
  • 146
  • 174