Cara menggunakan slicing multidimensional list python

Python lists are used to store a collection of items. A single python list can store many different data types. You can also apply different types of functions to Python lists. For example, you can count the number of items in a list, you can add or subtract all the integers in a list of integers, you can insert, remove, and even sort list items. Today we’re going to explore how to apply list slicing to Python lists.

Python list slicing refers to the process of extracting subsets of a Python list by slicing the original list. Python lists can be of two types: One-dimensional lists and multidimensional lists. We’re going to show you how to apply slicing to both of these list types.

The best way to get comfortable with Python list slicing is by running a lot of examples. That’s precisely what we intend to do with this tutorial.

Slicing One-dimensional Python lists

A one dimensional list is a list in the form of a row. Here’s an example of a one-dimensional list:

my_list = ["A","B","C","D","E","F","G","H","I","J"]

Lists in Python follow zero-based indexing which means that the first item is stored at the 0th index while the last item is stored at N-1 index where N is the total number of items in a list. In the example above,Awould be accessed with

my_slice = my_list[2:7]
print[my_slice]
7 andJwould be accessed with
my_slice = my_list[2:7]
print[my_slice]
8.

To apply list slicing to a one-dimensional list, the following syntax is used.

sliced_list = list[start_index: end_index+1: step]

As shown in the above syntax, to slice a Python list, you have to append square brackets in front of the list name. Inside square brackets you have to specify the index of the item where you want to start slicing your list and the index + 1 for the item where you want to end slicing. The step parameter is optional and defines the number of items to skip while slicing the list. A step of 2 will skip 1 item and will return alternating items.

Slicing in the middle of a list

Let’s take a look at an example. In the following script we create a list of 10 items. The items are the first 10 letters of the English alphabet. Again, the first letterAis located at 0th index while the last letterJis located at the 9th index.

my_list = ["A","B","C","D","E","F","G","H","I","J"]

To slice the above list from letters C to G, follow this process. SinceCis located at the 2nd index, we pass 2 as the start index and sinceGis located at the 6th index, we pass 6 + 1 = 7 as the end index.

my_slice = my_list[2:7]
print[my_slice]

Output:

['C', 'D', 'E', 'F', 'G']

Let’s now slice a list from C to G but with a step of 2.

my_slice = my_list[2:7:2]
print[my_slice]

Studying the output below, you’ll notice 1 item is skipped between each letter and only alternating itemsCtoGare returned.

Output:

['C', 'E', 'G']

A step of 3 will skip two items and will return every third item.

Slicing the beginning of a list

If you want to return the items from the first [the 0th] to Nth index, do not pass any value for the start index and only pass the value for the end index. The following script returns all the values from the start of your list to the 6th index.

my_slice = my_list[:7]
print[my_slice]

Output:

['A', 'B', 'C', 'D', 'E', 'F', 'G']

Slicing the end of a list

In the same way, to slice a list starting from a specific index to the end of your list, only pass the start index and leave the end index empty. For example, the following script returns all the list items starting from the 2nd index till the end of list.

my_slice = my_list[2:]
print[my_slice]

Output:

sliced_list = list[start_index: end_index+1: step]
0

Slicing your list relative to the last item

You can also pass negative indices to slice a list. In negative indexing, the last index is considered as -1. In the following script the starting index is set to -7 while the end index is set to -2.

sliced_list = list[start_index: end_index+1: step]
1

The letter at the -7th index of the list isD, while the letter just before the -2 index isH. Just like with positive indexing, the list slicing stops at the index just before the number you put in your slice. In this instance,His the -3 index, so in the output of the code above, you’ll see a list of items fromDtoH:

Output:

sliced_list = list[start_index: end_index+1: step]
2

You can also you use negative index to slice a list from the beginning or end.

For example, to slice a list from the beginning up to but not including the -2 index, use the following code.

sliced_list = list[start_index: end_index+1: step]
3

Output:

sliced_list = list[start_index: end_index+1: step]
4

Similarly, to slice a list starting from index number -7 to the end of the list, the following script can be used.

sliced_list = list[start_index: end_index+1: step]
5

Output:

sliced_list = list[start_index: end_index+1: step]
6

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Apa itu slicing pada python?

Slicing merupakan teknik memilih data dari sebuah set data. Misal kita memiliki data berat badan mahasiswa: 65, 78, 77, 100, 56. Maka jika kita urutkan maka urutan pertama adalah 65, urutan kedua adalah 78, urutan ketiga adalah 77, urutan keempat adalah 100, dan urutan terakhir adala 56.

Apa itu array di Python?

Array python adalah struktur data seperti list. Array berisi sejumlah objek yang dapat terdiri dari tipe data yang berbeda yang dapat diulang dan memiliki fungsi given untuk mengolahnya. Array digunakan untuk menyimpan, mengatur, dan mengelola data.

Bài mới nhất

Chủ Đề