Python Forum
How do you find the length of an element of a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you find the length of an element of a list?
#1
How do list the length of each string in a list?

Here is the question.

"Create a function that takes a list of words and transforms it into a list of each word's length.

Examples
word_lengths(["hello", "world"]) ➞ [5, 5]

word_lengths(["Halloween", "Thanksgiving", "Christmas"]) ➞ [9, 12, 9]

word_lengths(["She", "sells", "seashells", "down", "by", "the", "seashore"]) ➞ [3, 5, 9, 4, 2, 3, 8]

Notes
No test case will contain punctuation.
Lists can be of various lengths.
"

I don't care if someone tells me what it is. My goal is to learn something from it. I can't seem to find anything online.
Reply
#2
It surprises me that you couldn't find anything from a search, but oh well. In any case, if you're not allowed to use built in functions, how would you count the number of characters in a string? That should be pretty straightforward to work out yourself.
Reply
#3
I would use len(). I tried len() but I can't figure it out at all. It wants me to find the length of each string/element in a list or whatever. That's where I'm lost. It can be any given number of strings in the list. How do I count them all individually and output each count in list format like above? I poorly worded my question. I need more help than just figuring out to use len().
Reply
#4
Which kind of statement do you normally use to go through the items in a list?
Reply
#5
(Jun-11-2020, 04:01 AM)ndc85430 Wrote: Which kind of statement do you normally use to go through the items in a list?

str()
Reply
#6
What? For a start, str is a function not a statement and it also doesn't have anything to do with lists. Let's try a different way: if you have a list, how would you print each item?
Reply
#7
for x in range(len(a)): 
    print a[x]
Reply
#8
Ok, so taking your list of words, could you print the length of each one?
Reply
#9
(Jun-11-2020, 04:12 AM)ndc85430 Wrote: Ok, so taking your list of words, could you print the length of each one?

Not sure by using this one. I did learn today that there is a built-in function called list(). This problem is still really confusing me.
Reply
#10
(Jun-11-2020, 04:09 AM)pav1983 Wrote:
for x in range(len(a)): 
    print a[x]

Quick point that may help understanding. Whenever you are using range(len(xxx)) there is always a better way. In this case
for element in a:
gives you element, which is each item in the list. Get the len of element and append it to a second list that is your output. So, you need to define that second output list, change your loop, and return the output list.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing all strings in a list that are of x length Bruizeh 5 3,187 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
Question Recursion and permutations: print all permutations filling a list of length N SantiagoPB 6 3,285 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,338 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,255 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  Find first letter from a list DariusCG 3 2,509 Nov-27-2019, 11:08 PM
Last Post: ichabod801
  How to assign a "Score" variable to each element in a list Reta 5 5,329 May-16-2019, 01:13 PM
Last Post: ichabod801
  Find 'greater than' items in list johneven 2 4,469 Apr-05-2019, 07:22 AM
Last Post: perfringo
  Extracting list element with user input valve 1 2,583 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  Print The Length Of The Longest Run in a User Input List Ashman111 3 3,203 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk
  maximum and minimum element from the list and return output in dict MeeranRizvi 1 3,744 Jan-02-2017, 02:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020