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?
#11
First thing is that your loop will work but in python there is a better way to loop over list (and other iterable sequences). Instead of forcing an index like you did:
for x in range(len(a)): 
    print a[x]
It's more pythonic to simply loop over the items themselves:
for x in a: 
    print a
I should mention now that 'a' is a really bad variable name and you should get in the habit of using meaningful names. Even "my_list" would do. Also, do you have to use python 2? It's not being supported anymore so unless it's required by your class you should upgrade to python 3.

So back to your problem. Can you see how easy it would be to print the length of each word? If you can print those words and you know how to use len()…

Then, you will want to create a new list with all the lengths in it. So you will need to start with an empty list:
my_length_list = [] # Empty brackets create an empty list.
Put that before your loop to initialize your my_length_list each time the program runs.
Inside the loop you will need to add the length of the word to the end of your my_length_list by appending (hint!) it.

See how far you get and let us know if you get stuck...
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#12
(Jun-12-2020, 04:02 PM)Marbelous Wrote: First thing is that your loop will work but in python there is a better way to loop over list (and other iterable sequences). Instead of forcing an index like you did:
for x in range(len(a)): 
    print a[x]
It's more pythonic to simply loop over the items themselves:
for x in a: 
    print a
I should mention now that 'a' is a really bad variable name and you should get in the habit of using meaningful names. Even "my_list" would do. Also, do you have to use python 2? It's not being supported anymore so unless it's required by your class you should upgrade to python 3.

So back to your problem. Can you see how easy it would be to print the length of each word? If you can print those words and you know how to use len()…

Then, you will want to create a new list with all the lengths in it. So you will need to start with an empty list:
my_length_list = [] # Empty brackets create an empty list.
Put that before your loop to initialize your my_length_list each time the program runs.
Inside the loop you will need to add the length of the word to the end of your my_length_list by appending (hint!) it.

See how far you get and let us know if you get stuck...

Actually, this is not for a class. I don't know why this post was moved to homework. I'm trying to get the answer and understand it. This is the code I came up with afterwords. This problem is on Edabit. It seems Edabit is a bit difficult for Python. 'Very easy' doesn't seem very easy. I think it's 'very easy' for experienced programmers.

def word_lengths(lst):
	lst = []
	for x in range(len(lst))
  return lst[x]
That code, however, still doesn't work.

I desperately need a mentor. I figure things out better by repetition and doing.
Reply
#13
Well it's good that you are creating a function and you started off OK except you should realize that the very first thing you do in the function is erase the list (lst) that you need to iterate over. You will require two lists for this task. One list you will loop over and read to get the words to process. Another list you will write to every time through the loop to store the length of each word. So you will iterate through the list of words you are given and each time through you will add a new item to the new list you created to store all the lengths of the words. When you are done the lists should be the same size.

Now please stop using range() and iterate over the items the way python is designed to. Look at this code snippet:

word_list = ["one", "two", "three", "four"]

for word in word_list:
    print word
    print len(word)
It is much easier and less confusing to loop through the list by the items in it than to use range and have to index each item. This a one of the best things about python so take advantage!

Now you want to create a new, empty list and fill it with the length of each word while you loop through it and you already know how to initialize it:

word_list = ["one", "two", "three", "four"]

length_list = [] # Creates a new empty list to fill with the length of each word.
for word in word_list: # See, no using range() or len() needed.
    print word # And no need to index either.  The variable 'word' updates automatically on each loop!
    print len(word)
    length_list.______(len(word)) # You need the function that adds an item to a list.
print length_list # Print out your list of lengths.

Now you just need to look in the python docs for that function (I already gave you a hint) and then convert the whole thing into a function like you were doing so that you return the list instead of printing it.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#14
I gave up and just unlocked the solution. I hate to do it, but it's how you learn sometimes. I wouldn't have ever figured it out on my own. At least the positive is that I learned something new. I have completed most of the challenges successfully anyway, so unlocking one once in a while won't kill me.

def word_lengths(lst):
  return [len(x) for x in lst]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing all strings in a list that are of x length Bruizeh 5 3,184 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,284 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,335 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,252 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,326 May-16-2019, 01:13 PM
Last Post: ichabod801
  Find 'greater than' items in list johneven 2 4,465 Apr-05-2019, 07:22 AM
Last Post: perfringo
  Extracting list element with user input valve 1 2,582 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