Python Forum
Why do I have to repeat items in list slices in order to make this work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do I have to repeat items in list slices in order to make this work?
#1
Hello everyone, I was testing out making a f strings with parts of a list, and after experimenting a lot, I figured out how to make it work but I am confused why:

#contains a different message for each portion of the list.
distros = ['Ubuntu','Mint','Kali','Fedora','CentOS','Oracle','Mageia',\
'KateOS','Slackintosh']

for debian in distros[:3]:
  print(f"{debian} is a debian distribution.")

for Red_Hat in distros[3:6]:
  print(f"{Red_Hat} is a Red Hat distribution.")

for slackware in distros[6:]:
  print(f"{slackware} is a slackware distribution.")
That gives me the proper output:
Ubuntu is a debian distribution.
Mint is a debian distribution.
Kali is a debian distribution.
Fedora is a Red Hat distribution.
CentOS is a Red Hat distribution.
Oracle is a Red Hat distribution.
Mageia is a slackware distribution.
KateOS is a slackware distribution.
Slackintosh is a slackware distribution.
but I am confused why I am using repeating numbers in the above code to accomplish that: for the Red Hat distributions, why am I starting with 3 even though the last cycle ended with 3, and then with the slackware distributions, why am i starting with 6 even though the last cycle ended with 6? To me, the last two should be [4:6] and [7:9}, but that gives me incorrect results.
Reply
#2
Slicing starts at the start number and ends before the end number. I think this is because indexing starts at 0.
things = "abcdefg"
print(things[:3])  # Print the first 3 letters.
If the slice contained the ending value you would write this:
things = "abcdefg"
print(things[:2])  # Print the first 3 letters.
Hmmm. I want three values, but I need to ask for 2?

range() use the same protocol.
for i in range(3):  # I want to iterate 3 times
    print(i)
Output:
0 1 2
Similar to slicing, range would look odd if the range included the end number.
for i in range(2):  # I want to iterate 3 times.  Why do I need to enter 2?
    print(i)
When you start with zero, the number in range(number) equals the number of iterations. You can set the start number to any value, but by a huge margin most range(start, end, increment) skip setting the start and increment.
Pythonica likes this post
Reply
#3
The great advantage of not including the element indexed by the upper bound of a slice is that when a <= b <= c
thing[a:b] + thing[b:c] == thing[a:c]
Reply
#4
I think indexing is best avoided when possible.
distros = {
    "debian": ("Ubuntu", "Mint", "Kali"),
    "Red Hat": ("Fedora", "CentOS", "Oracle"),
    "slackware": ("Mageia", "KateOS", "Slackintosh"),
}

for vendor in distros:
    for distro in distros[vendor]:
        print(f"{distro} is a {vendor} distro.")
Output:
Kali is a debian distro. Fedora is a Red Hat distro. CentOS is a Red Hat distro. Oracle is a Red Hat distro. Mageia is a slackware distro. KateOS is a slackware distro. Slackintosh is a slackware distro.
Pythonica likes this post
Reply
#5
all_distros = {"debian": ("Ubuntu", "Mint", "Kali"),
               "Red Hat": ("Fedora", "CentOS", "Oracle"),
               "slackware": ("Mageia", "KateOS", "Slackintosh")}
 
for vendor, distros in all_distros.items():
    for distro in distros:
        print(f"{distro} is a {vendor} distro.")
Just a small improvement over last @deanhystad suggestion
Pythonica likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(May-19-2023, 04:21 PM)deanhystad Wrote: Slicing starts at the start number and ends before the end number. I think this is because indexing starts at 0.

Yeah that's what i was looking for.

@buran @deanhystad
all_distros = {"debian": ("Ubuntu", "Mint", "Kali"),
               "Red Hat": ("Fedora", "CentOS", "Oracle"),
               "slackware": ("Mageia", "KateOS", "Slackintosh")}
  
for vendor, distros in all_distros.items():
    for distro in distros:
        print(f"{distro} is a {vendor} distro.")
That's neat, is that a compound list or something?
Reply
#7
Dictionary. You will need to learn about dictionaries. A very important part of Python.
Pythonica likes this post
Reply
#8
Slicing starts at the start number and ends before the end number. And indexing starts at 0.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 205 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Copying the order of another list with identical values gohanhango 7 1,171 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  hi need help to make this code work correctly atulkul1985 5 801 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  tuple indices must be integers or slices, not str cybertooth 16 11,618 Nov-02-2023, 01:20 PM
Last Post: brewer32
  newbie question - can't make code work tronic72 2 696 Oct-22-2023, 09:08 PM
Last Post: tronic72
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,185 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Beginner: Code not work when longer list raiviscoding 2 832 May-19-2023, 11:19 AM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,434 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Finding combinations of list of items (30 or so) LynnS 1 885 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  How to work with list kafka_trial 8 2,052 Jan-24-2023, 01:30 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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