Skip to main content
  1. Posts/

Display all Items in a List

·614 words·3 mins

In this content I will show you how to display a item in a list, there is two ways you can do it and by the end of this day you will be able to achieve how to make it look like this.

Koder toys are a: cat, bear, tiger.

The program that uses join #

First you want to make a list with any names beside the keyword otherwise it won’t work after you finish you want to use print(print is a function to print it is simple to use) and write something in it like my favourite toy are:

>>> kodertoys = ['cat', 'bear', 'tiger']
>>> print('Koders toys are a:')
Koders toys are a:

The next step is to use the string function call .join this the hard part. So how it works is that join is when it takes all the item in a interable object (e.g. list or tuple), and join them together seperated by a string.

Note: iterable object (is an object that can be looped over).

Let’s use the function .join and str().

And here is how it’s done

>>> kodertoys = ['cat', 'bear', 'tiger']
>>> print('Koders toy are a:')
Koders toy are a:
>>> print('Koder toys are a:', ', '.join([str(k) for k in kodertoys]))
Koder toys are a: cat, bear, tiger
>>>

It looks like we still need to add a fullstop at the end of the line.

>>> print('Koder toys are a:', ', '.join([str(k) for k in kodertoys]), end='.\n')
Koder toys are a: cat, bear, tiger.

Bonus: My dad actually showed me another way to do this:

>>> print(f"Koder's toys are: { ', '.join(kodertoys) }.")
Koder's toys are: cat, bear, tiger.

The program that doesn’t uses join and doesn’t have a message: #

In this program the code is shorter and easier and it is recommended for you to use.

The goal is to print out all items in the list except the last one. Then we print the last item by itself followed by a fullstop (.).

In Python, fruits[0:-1] is a subset(a little amount) of the fruits list starting from index 0 to the -1 (last item), BUT not including the last item.

fruits = ['grape', 'apple', 'banana', 'orange', 'apple', 'strawberry']

for f in fruits[:-1]:
    print(f, end=', ') 

print(fruits[-1], end='.')

So the first thing you want to do is to make a list just like the first instruction but this time don’t print a message.

fruits = ['grape', 'apple', 'banana', 'orange', 'apple', 'strawberry']

Then the next step is to use a for loop however by the end of this you will have something quite not what you want but then you want to add brackets and a colon and -1 (because -1 is equal to the last item).

but the reason of taking the last item of the list is because we want to take it away add a full stop then add it back to the python code we use end because it goes to all the last letter of each word and add a comma and a space so if we took the last item out of the list it wouldn’t have a comma at the end.

for f in fruits[:-1]:
    print(f, end=', ') 

The last step is to add back the last item and and a full stop so we use end as I said before end goes to the last letter of the word and doesn’t make a new line.

print(fruits[-1], end='.')

and it will look like this

grape, apple, banana, orange, apple, strawberry.

In the end no matter if you add a item it will have a full stop and the old last item will have a comma like the others.