Slice list python | Laptrinhcanban.com

HOME › >>

Slice list python

Posted at: https://laptrinhcanban.com/en

Instructions on how to slice list python. You will learn what is the concept of slice list python, what is the index of the elements in a list , as well as how to truncate list in python using indexes after this lesson.

What is slice list python

The slice list in Python is the manipulation of getting a number of elements from a list .

To slice list in python, we need to specify the starting and ending position of the cut, using the following syntax:

list [start position : end position]

Inside,

  • List : initial list
  • start position : the index of the element at the beginning of trimming
  • end position : the index of the element at the end of truncation plus 1 unit.

Note that the end position is equal to index of these element at end position + 1, because when using python’s slice function, the cut out only includes the element in the original cut position to before the element at the end position, not before. includes the element at the end position. So if we want to get the element at the end position, we need to specify an extra unit.

For example we have a list of numbers nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and want to cut out the elements from 1 to 5, we will write the following:

nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
newlist = nums [ 0 : 5 ]

print (nums)
print (newlist)

Result:

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

What is the index of the elements in the list

In the above section appears the concept of index, also known as the index of the elements in the list , so it can make many of you confused.
The index of elements in the list is understood as the ordinal number or the position of an element in the list containing it. Each element will have only its own index and unique in the list.
Hence we can slice the list or access elements in the list through this index.
There are two ways of defining an index:

  • Index is positive from left to right, starting with 0 and increasing by 1
  • Index is negative from right to left, starting with -1 and decreasing by 1 unit

For example, we define the indexes of the list ["Hà Nội", "Sài Gòn", "Đà Nẵng"] as follows:

Can remember simply the index in python if counting from the beginning then start counting from 0 and if counting from the end, it will start counting from -1 .

In the next section we will learn how to slice list in python using indexes.

Slice list in python by specifying the cut off range

By specifying the starting position and the index ending position of the elements at those positions, we can slice list in python like in the following example:

nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
print (nums [ 2 : 5 ])
print (nums [- 5 : - 2 ])

Result:

[3, 4, 5] 
[6, 7, 8]

Cut the list in python from the top of the list to the middle of the list

To slice list in python from the top of list to the middle of list, we omit its start position when writing list cut syntax.
For example we write [:5]. Then, python implicitly implicitly starts with 0 position and cuts list similar to when writing [0: 5] as following example:

nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
newlist1 = nums [ 0 : 5 ]
newlist2 = nums [: 5 ]

print (newlist1)
print (newlist2)

Result:

[1, 2, 3, 4, 5] 
[1, 2, 3, 4, 5]

Slicelist in python from the middle of list to the end of list

To slice list in python from the middle of list to the end of list, we omit its end position when writing list cut syntax.
For example we write [5:]. Then, python implicitly implicitly ends with the number of elements of the list (can be calculated by the len () function) as shown in the following example:

nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
newlist3 = nums [ 5 :]
newlist4 = nums [ 5 : 10 ]
newlist5 = nums [ 5 : len (nums)]

print (newlist3)
print (newlist4)
print (newlist5)

Result:

[6, 7, 8, 9, 10] 
[6, 7, 8, 9, 10]
[6, 7, 8, 9, 10]

Slice list in python by specifying step

When specifying a step when slicing list in python, we can cut out elements with index separated by the number of steps, like the following example:

nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
print (nums [:: 1 ])
print (nums [:: 2 ])
print (nums [:: 3 ])

Result:

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

We can also specify a step with a start and end position as follows:

nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
print (nums [ 2 : 7 : 1 ])
print (nums [ 2 : 7 : 2 ])
print (nums [- 7 : - 2 : 3 ])

Result:

[3, 4, 5, 6, 7] 
[3, 5, 7]
[4, 7]

Cut back the list in python

When specifying a starting and ending position with a negative index, we can invert a list in python like in the following example:

print (nums [:: - 1 ]) 
print (nums [:: - 2 ])

Result:

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

We also have an inverted slice list in python with a start and end position as follows:

nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
print (nums [ 8 : 2 : - 1 ])
print (nums [- 2 : - 9 : - 2 ])

Result:

[9, 8, 7, 6, 5, 4] 
[9, 7, 5, 3]

Parse a list by trimming the list in python

With the application of cutting lists in python, we can split a list into many different parts. In the following example, we will split an original list into two smaller lists:

n = 4
nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]

nums1 = nums [: n]
nums2 = nums [n:]

print (nums1)
print (nums2)

Result:

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

Summarize

Above, Kiyoshi showed you how to slice list in python. To better understand the lesson content, practice rewriting today’s examples.

And let’s learn more about Python in the next lessons.

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/list-trong-python/cat-lat-list-trong-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.