Python Forum

Full Version: Why do I have to repeat items in list slices in order to make this work?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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]
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.
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
(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?
Dictionary. You will need to learn about dictionaries. A very important part of Python.
Slicing starts at the start number and ends before the end number. And indexing starts at 0.