448,548 Members | 1,183 Online
Need help? Post your question and get tips & solutions from a community of 448,548 IT Pros & Developers. It's quick & easy.

# 'str' object has no attribute 'append'

 P: 5 Expand|Select|Wrap|Line Numbers cars = ['honda' , 'ford' , 'kia' , 'toyota' , 'jaguar' , 'daimler' , 'citroen' , 'peugeot' , 'skoda']   # Print the longest and the shortest words in the gunners list shortest=[]   longest=[] shortest=[] for i in cars:     if len(i) > len(longest):         longest = i print longest   for i in cars:     shortest = cars[0]     if len(i) < len(shortest):             shortest = [i]     elif len(i) == len(shortest):         shortest = shortest.append[i] print shortest when i try to execute the code i get the above error on the shortest = shortest.append[i] line. i'm new to programming so go easy! thanks!! Feb 22 '15 #1
4 Replies

 Expert Mod 2.5K+ P: 2,851 You only need to initialize shortest once. You initialize a list named longest, but in the for loop you assign longest to a string. You are mixing up your data types in the next for loop. Calling list method append will append an object to the list in place and return None, so your assignment to shortest will get you nowhere. Try something like this: Expand|Select|Wrap|Line Numbers >>> cars = ['honda' , 'ford' , 'kia' , 'toyota' , 'jaguar' , 'daimler' , 'citroen' , 'peugeot' , 'skoda'] >>> lenList = [len(car) for car in cars] >>> for car in cars: ...     if len(car) == min(lenList): ...         print "Shortest: %s" % car ...     if len(car) == max(lenList): ...         print "Longest: %s" % car ...          Shortest: kia Longest: daimler Longest: citroen Longest: peugeot >>>  Feb 22 '15 #2

 Expert Mod 2.5K+ P: 2,851 hi i've tried your code and it makes sense. thanks very much. i'm still getting an error though. now its telling me: len(list) = [len(car) for car in cars] Syntax Error:cant assign to function call. any ideas? thanks again anthony Apparently you misread my post. The list comprehension (code on the right side of the "=" character) must be assigned to an identifier. len(list) is not a valid identifier and actually returns the following when executed: Expand|Select|Wrap|Line Numbers >>> len(list) Traceback (most recent call last):   File "", line 1, in  TypeError: object of type 'type' has no len() >>>  Feb 23 '15 #3

 P: 5 sorry i dont understand. I'm new to programming. What do i need to do to fix it? Feb 23 '15 #4

 Expert Mod 2.5K+ P: 2,851 You are creating a list to use in determining the length of the longest and shortest strings. You must assign this list to an identifier. This is a very basic concept that you need to understand. A simple example: Expand|Select|Wrap|Line Numbers >>> a = 1 >>> b = 2 >>> a+b 3 >>>  So in your case, create the list and assign to a VALID identofier similar to a and b in the example. Expand|Select|Wrap|Line Numbers c = [len(car) for car in cars] Now you have access to the list by referring to c. Use a descriptive name so you can recognize its purpose like "list_of_word_lengths". A valid identifier must start with a letter or an underscore. a-z A-Z _ After that, the identifier can use a letter, digit(0-9) or underscore. Feb 23 '15 #5