Python Forum
slicing and indexing a list example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
slicing and indexing a list example
#1
I have the following code:

s = "The quick brown fox jumps over the lazy dog."
words = s.split()
lengths = [[w.lower(), len(w)] for w in words]
for length in lengths:
 print(length)
The output is:

['the', 3]
['quick', 5]
['brown', 5]
['fox', 3]
['jumps', 5]
['over', 4]
etc.

I wish to change it to produce the lengths only, namely the numbers.
Also, is this an example of lists inside lists (nested lists)?
I am trying to
Reply
#2
In your comprehension, you've asked the list to be assembled from [w.lower(), len(w)]. So the elements are: a list, with the lowercasename first, and the length second.

If you just want the lengths, just put the lengths there. Change the line to:

lengths = [len(w) for w in words]
Reply
#3
But you should also pay attention to output of your code. Is 'dog' 4 letter word?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Oct-01-2020, 02:12 AM)bowlofred Wrote: In your comprehension, you've asked the list to be assembled from [w.lower(), len(w)]. So the elements are: a list, with the lowercasename first, and the length second.

If you just want the lengths, just put the lengths there. Change the line to:

lengths = [len(w) for w in words]


Let me amend my question. I wanted to access every length inside each of the outputs.
How do I do this indexing since each output is a list containing 2 elements?
So I tried the following:
print(length[1:])
Reply
#5
I'm afraid I can't understand exactly what you're asking. Can you show what you expect the output to be?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Indexing problem while iterating list and subtracting lbtdne 2 2,087 May-14-2020, 10:19 PM
Last Post: deanhystad
  TypeError indexing a range of elements directly on the list JFerreira 2 2,167 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  How to change 0 based indexing to 1 based indexing in python..?? Ruthra 2 4,275 Jan-22-2020, 05:13 PM
Last Post: Ruthra
  Help with slicing a list FWendeburg 3 2,550 Dec-02-2019, 04:57 PM
Last Post: michael1789
  "Up to but not including" (My personal guide on slicing & indexing) Drone4four 5 2,823 Nov-20-2019, 09:38 PM
Last Post: newbieAuggie2019
  List slicing issue Irhcsa 3 2,928 Apr-26-2019, 09:16 PM
Last Post: nilamo
  Slicing Python list of strings into individual characters Drone4four 5 3,426 Apr-17-2019, 07:22 AM
Last Post: perfringo
  String Slicing in List Comphrensions Patroclus72790 1 2,228 Mar-21-2019, 09:33 PM
Last Post: nilamo
  Slicing a complex dictionary and list Drone4four 2 6,723 Aug-16-2018, 02:27 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