python - List Index Out of Range: Importing info from two lists into one conditionally -


a = "alpha" b = none c = "charlie" first_list = [a,b,c]  a_define = "a" b_define = "b" c_define = "c" second_list = [a_define, b_define, c_define]  third_list = []  len_of_firstlist = len(first_list)  item in range(len_of_firstlist+1):     if first_list[item] not none:         first_list[item] = first_list[item] + second_list[item]         third_list.append(first_list[item])  print third_list 

this might seem bit generic unfortunately, i'm lost... code seems work, keeps giving me indexing error.

i think unclear of these points :

  • range(start=0,stop,step=1) function starts default value 0 , stop when value equal stop i.e. stop value not included. further information, see docs.
  • lists in python indexed 0 (like arrays in c).

so if want traverse string length n, need define range range(n).

if want more compact code, few changes might be:

a = "alpha" b = none c = "charlie" first_list = [a,b,c]  a_define = "a" b_define = "b" c_define = "c" second_list = [a_define, b_define, c_define]  third_list = []  item in xrange(len(first_list)):     if first_list[item] not none:         first_list[item] += second_list[item]         third_list.append(first_list[item])  print third_list 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -