Cara menggunakan python slice list

In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:)

With this operator, one can specify where to start the slicing, where to end, and specify the step. List slicing returns a new list from the existing list.

Syntax:

Lst[ Initial : End : IndexJump ]

If Lst is a list, then the above expression returns the portion of the list from index Initial to index End, at a step size IndexJump.

Indexing

1. Positive Indexes

Cara menggunakan python slice list

Below is a simple program, to display a whole list using slicing.

Python3

Lst = [50, 70, 30, 20, 90, 10, 50]

print(Lst[::])

Output:

[50, 70, 30, 20, 90, 10, 50]

The above diagram illustrates a list Lst with its index values and elements.

2. Negative Indexes

Now, let us look at the below diagram which illustrates a list along with its negative indexes.

Index -1 represents the last element and -n represents the first element of the list(considering n as the length of the list). Lists can also be manipulated using negative indexes also.

Python3

Lst = [50, 70, 30, 20, 90, 10, 50]

print(Lst[-7::1])

Output:

[50, 70, 30, 20, 90, 10, 50]

The above program displays the whole list using the negative index in list slicing.

3. Slicing

As mentioned earlier list slicing is a common practice in Python and can be used both with positive indexes as well as negative indexes. The below diagram illustrates the technique of list slicing:

The below program transforms the above illustration into python code:

Python3

Lst = [50, 70, 30, 20, 90, 10, 50]

print(Lst[1:5])

Output:

[70, 30, 20, 90]

Below are some examples which depict the use of list slicing in Python:

Example 1:

Python3

List = [1, 2, 3, 4, 5, 6, 7, 8, 9]

print("\nOriginal List:\n", List)

print("\nSliced Lists: ")

print(List[3:9:2])

print(List[::2])

print(List[::])

Output:

Original List:
 [1, 2, 3, 4, 5, 6, 7, 8, 9]

Sliced Lists: 
[4, 6, 8]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Leaving any argument like Initial, End or IndexJump blank will lead to the use of default values i.e 0 as Initial, length of list as End and 1 as IndexJump.

Example 2:

Python3

List = ['Geeks', 4, 'geeks !']

print("\nOriginal List:\n", List)

print("\nSliced Lists: ")

print(List[::-1])

print(List[::-3])

print(List[:1:-2])

Output:

Original List:
 ['Geeks', 4, 'geeks !']

Sliced Lists: 
['geeks !', 4, 'Geeks']
['geeks !']
['geeks !']

A reversed list can be generated by using a negative integer as the IndexJump argument. Leaving the Initial and End as blank. We need to choose the Initial and End value according to a reversed list if the IndexJump value is negative. 

Example 3:

Python3

List = [-999, 'G4G', 1706256, '^_^', 3.1496]

print("\nOriginal List:\n", List)

print("\nSliced Lists: ")

print(List[10::2])

print(List[1:1:1])

print(List[-1:-1:-1])

print(List[:0:])

Output:

Original List:
 [-999, 'G4G', 1706256, '^_^', 3.1496]

Sliced Lists: 
[]
[]
[]
[]

If some slicing expressions are made that do not make sense or are incomputable then empty lists are generated.

Example 4:

Python3

List = [-999, 'G4G', 1706256, 3.1496, '^_^']

print("\nOriginal List:\n", List)

print("\nSliced Lists: ")

List[2:4] = ['Geeks', 'for', 'Geeks', '!']

print(List)

List[:6] = []

print(List)

Output:

Original List:
 [-999, 'G4G', 1706256, 3.1496, '^_^']

Sliced Lists: 
[-999, 'G4G', 'Geeks', 'for', 'Geeks', '!', '^_^']
['^_^']

List slicing can be used to modify lists or even delete elements from a list.

Example 5:

Python3

List = [1, 2, 3, 4, 5, 6, 7, 8, 9]

print("\nOriginal List:\n", List)

print("\nSliced Lists: ")

newList = List[:3]+List[7:]

print(newList)

List = List[::2]+List[1::2]

print(List)

Output:

Original List:
 [1, 2, 3, 4, 5, 6, 7, 8, 9]

Sliced Lists: 
[1, 2, 3, 8, 9]
[1, 3, 5, 7, 9, 2, 4, 6, 8]

By concatenating sliced lists, a new list can be created or even a pre-existing list can be modified. 


Can you slice a list of lists in Python?

With this operator, one can specify where to start the slicing, where to end, and specify the step. List slicing returns a new list from the existing list. If Lst is a list, then the above expression returns the portion of the list from index Initial to index End, at a step size IndexJump.

Can we use slice in list?

As well as using slicing to extract part of a list (i.e. a slice on the right hand sign of an equal sign), you can set the value of elements in a list by using a slice on the left hand side of an equal sign. In python terminology, this is because lists are mutable objects, while strings are immutable.

How do you do slicing in Python?

Python slice() Function The slice() function returns a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.

How do you cut a list in half in Python?

This can be done using the following steps:.

Get the length of a list using len() function..

If the length of the parts is not given, then divide the length of list by 2 using floor operator to get the middle index of the list..

Slice the list into two halves using [:middle_index] and [middle_index:].

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 yang dimaksud dengan list di Python?

List adalah tipe data yang paling serbaguna yang tersedia dalam bahasa Python, yang dapat ditulis sebagai daftar nilai yang dipisahkan koma (item) antara tanda kurung siku. Hal penting tentang daftar adalah item dalam list tidak boleh sama jenisnya.

Apa itu append pada Python?

Fungsi append menambahkan nilai array pada urutan akhir. Sedangkan dengan fungsi insert kita bisa menambahkan nilai array pada posisi tertentu.

Langkah cepat untuk menjalankan script program pada aplikasi idle Python yaitu?

Menjalankan Python.
Buka terminal CTRL + ALT + T..
Ketik python maka Anda akan masuk ke Python shell..
Tuliskan script Python Anda, contoh: print("Selamat datang di Python") . jika sudah tekan tombol ENTER , dan script Python akan dijalankan/eksekusi..
Untuk keluar dari Python shell ketik exit().