Slicing lists in Python

Slicing in Python means cutting out a part of a list you wish to use in another part of a program. Based on indices of the items in a list, we can slice lists.

For example, by taking the list we created and calling my_list, we can slice it. Look at the following codes closely:

my_list = ["Hey", 3, 4.5, False, 2.0, 'Me', True]
print (my_list [2:5])

Here is the result:

[4.5, False, 2.0]

my_list = ["Hey", 3, 4.5, False, 2.0, 'Me', True]
print (my_list [:])

Here is the result:

[‘Hey’, 3, 4.5, False, 2.0, ‘Me’, True]

In the same way, we can Replace anything in the list with a new value.

For example, our list my_list:

my_list = ["Hey", 3, 4.5, False, 2.0, 'Me', True]
my_list [1] = 'Three'

print (my_list)

Look at the result:

[‘Hey’, ‘Three’, 4.5, False, 2.0, ‘Me’, True]

Apart from what we have done so far with lists, there are so many cool things you can do with Python lists. In the next tutorials, I will show you some cool programs that are done with lists that can blow your mind.

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials