python - how can I check the length of a list? -
i have list in python , want know number of "words"/"products" in it:
print shopping_list output: ['milk,cookies,apples']
now output should 3 1 , don't understand why :
print len(shopping_list) output: 1
note need 1 string contains words separated commas
your starting list is:
shopping_list = ['milk,cookies,apples'] this list contains single string 'milk,cookies,apples' need adjust list below.
shopping_list = ['milk','cookies','apples'] your code function expected. list of strings can used calculate length.
you can python split list using comma indicator of new item.
shopping_list = ['milk,cookies,apples'] shopping_list = shopping_list[0].split(",") print shopping_list return ['milk','cookies','apples'] note original shopping_list list need state want first item, index [0]
print len(shopping_list) will return 3.
Comments
Post a Comment