note.nkmk.me

How to slice a list, string, tuple in Python

Posted: 2020-01-30 / Tags: Python, List, String

In Python, a slice (slicing) represented by colons (eg: [2:5:2]) allows you to select items in a sequence object, such as a list, string, tuple, etc.

This post describes the following contents.

  • Basic usage of slicing
    • Start position start and end position stop
    • step
  • Select from the end with a negative value
    • Negative values for start and stop
    • Negative values for step
  • Slice object by slice()
  • Assigning values by slicing
  • Slicing for strings and tuples
Sponsored Link

Basic usage of slicing

Start position start and end position stop

In a slicing, the start position start and end position stop of the selection are written as [start:stop].

The range start <= x <stop is selected. Note that the item at start is included, but the item at stop is not included.

l = [0, 10, 20, 30, 40, 50, 60]
print(l)
# [0, 10, 20, 30, 40, 50, 60]

print(l[2:5])
# [20, 30, 40]

If start is omitted, the selection is made from the beginning, and if stop is omitted, the selection is made up to the end. If both are omitted, all items are selected.

print(l[:3])
# [0, 10, 20]

print(l[3:])
# [30, 40, 50, 60]

print(l[:])
# [0, 10, 20, 30, 40, 50, 60]

Out of range

No error occurs if you specify a position that exceeds the number of items. The case where a negative value is specified will be described later.

print(l[2:10])
# [20, 30, 40, 50, 60]

If no item is selected

No error occurs if you specify start and stop that select no item. Returns an empty list.

print(l[5:2])
# []

print(l[2:2])
# []

print(l[10:20])
# []

step

In addition to the start position start and end position stop, step can also be specified. Write it as [start:stop:step].

For example, if step is set to 2, items at odd-numbered positions or even-numbered positions can be selected.

print(l[::2])
# [0, 20, 40, 60]

print(l[1::2])
# [10, 30, 50]

Other examples:

print(l[::3])
# [0, 30, 60]

print(l[2:5:2])
# [20, 40]

As in the previous examples, if step is omitted, it is set to 1.

Select from the end with a negative value

Negative values for start and stop

If start and stop are specified with negative values, they are regarded as positions from the end.

-1 indicates the last item.

If stop=-1, the item at stop is not included, so the second value from the end is selected.

print(l[3:-1])
# [30, 40, 50]

Other examples:

print(l[-2:])
# [50, 60]

print(l[-5:-2])
# [20, 30, 40]

Negative values for step

If step is specified as a negative value, the items are selected in reverse order.

Items are selected from the position at start. Note that unless start indicates a position after stop, it will be empty.

print(l[5:2:-1])
# [50, 40, 30]

print(l[2:5:-1])
# []

Other examples:

print(l[-2:-5:-1])
# [50, 40, 30]

print(l[-2:2:-1])
# [50, 40, 30]

print(l[5:2:-2])
# [50, 30]

By omitting start and stop and setting step to -1, you can obtain a reversed object.

print(l[::-1])
# [60, 50, 40, 30, 20, 10, 0]

You can also use reverse() and reversed() to reverse lists or strings, tuples, etc. See the following post for details.

Slice object by slice()

You can generate a slice object using the built-in function slice(). If you want to repeatedly select the items at the same position, you only need to generate the slice object once.

slice(start, stop, step) is equivalent to start:stop:step.

sl = slice(2, 5, 2)
print(sl)
# slice(2, 5, 2)

print(type(sl))
# <class 'slice'>

print(l[sl])
# [20, 40]

If two arguments are specified, step is set to None. This is equivalent to start:stop.

sl = slice(2, 5)
print(sl)
# slice(2, 5, None)

print(l[sl])
# [20, 30, 40]

If only one argument is specified, start and step is set to None. This is equivalent to : stop.

sl = slice(2)
print(sl)
# slice(None, 2, None)

print(l[sl])
# [0, 10]

If all arguments are omitted, an error TypeError occurs.

# sl = slice()
# TypeError: slice expected at least 1 arguments, got 0

If you want to generate : with slice(), explicitly specify None.

sl = slice(None)
print(sl)
# slice(None, None, None)

print(l[sl])
# [0, 10, 20, 30, 40, 50, 60]
Sponsored Link

Assigning values by slicing

New values can be assigned to the range selected with slicing.

It does not matter if the number of items in the range selected with slicing does not match the number of items (= the length of object) to be assigned.

print(l)
# [0, 10, 20, 30, 40, 50, 60]

l[2:5] = [200, 300, 400]
print(l)
# [0, 10, 200, 300, 400, 50, 60]

l[2:5] = [-2, -3]
print(l)
# [0, 10, -2, -3, 50, 60]

l[2:4] = [2000, 3000, 4000, 5000]
print(l)
# [0, 10, 2000, 3000, 4000, 5000, 50, 60]

l[2:6] = [20000]
print(l)
# [0, 10, 20000, 50, 60]

Note that specifying a scalar value on the right side will result in an error TypeError.

# l[2:3] = 200
# TypeError: can only assign an iterable

If the right side is empty, the elements in the range selected by the slice will be deleted.

l[1:4] = []
print(l)
# [0, 60]

You can also assign to an empty range. The value on the right side is inserted at the specified position.

l[20:60] = [-1, -2, -3]
print(l)
# [0, 60, -1, -2, -3]

l[2:2] = [-100]
print(l)
# [0, 60, -100, -1, -2, -3]

If the number of elements is not equal for the range where step is specified, an error ValueError will occur.

print(l[:5:2])
# [0, -100, -2]

l[:5:2] = [100, 200, 300]
print(l)
# [100, 60, 200, -1, 300, -3]

# l[:5:2] = [100, 200]
# ValueError: attempt to assign sequence of size 2 to extended slice of size 3

To add an element in the middle or at the end of the list, methods such as insert() and append() are provided. See the following article:

Slicing for strings and tuples

So far, we have shown examples of lists (list type), but slices can be used with other sequence objects such as strings str and tuples tuple as well.

However, str and tuple are immutable, so new values cannot be assigned.

s = 'abcdefg'
print(s)
# abcdefg

print(s[2:5])
# cde

print(s[::-1])
# gfedcba

# s[2:5] = 'CDE'
# TypeError: 'str' object does not support item assignment

t = (0, 10, 20, 30, 40, 50, 60)
print(t)
# (0, 10, 20, 30, 40, 50, 60)

print(t[2:5])
# (20, 30, 40)

# t[2:5] = (200, 300, 400)
# TypeError: 'tuple' object does not support item assignment

See the following article for splitting and replacing strings.

Sponsored Link
Share

Related Categories

Related Posts