10

Possible Duplicate:
Good Primer for Python Slice Notation

I have a string and I'm splitting in a ; character, I would like to associate this string with variables, but for me just the first x strings is useful, the other is redundant;

I wanted to use this code below, but if there is more than 4 coma than this raise an exception. Is there any simply way?

az1, el1, az2, el2, rfsspe = data_point.split(";")  
Community
  • 1
  • 1
Kicsi Mano
  • 3,551
  • 3
  • 21
  • 26

2 Answers2

24

Yes! Use slicing:

az1, el1, az2, el2, rfsspe = data_point.split(";")[:5]

That "slices" the list to get the first 5 elements only.

Community
  • 1
  • 1
Cameron
  • 96,106
  • 25
  • 196
  • 225
6

The way, I do this is usually to add all the variables to a list(var_list) and then when I'm processsing the list I do something like

for x in var_list[:5]:
    print x #or do something
Lostsoul
  • 25,013
  • 48
  • 144
  • 239