Getting the first element x from a list

Possible duplicate:
A good primer to label Python fragments

I have a string and I am separating a character ;, I would like to associate this string with variables, but only the first lines of x are useful to me, the others are redundant;

I wanted to use this code below, but if there are more than 4 coms than this throws an exception. Is there an easy way?

az1, el1, az2, el2, rfsspe = data_point.split(";")  
+5
source share
2 answers

Yes! Use slicing :

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

This "cuts" the list to get only the first 5 items.

+14
source

, , , (var_list), , , -

for x in var_list[:5]:
    print x #or do something
+3

All Articles