Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. list + list / list * n / Slicing

list + list / list * n / Slicing

Operators and syntax for concatenating, repeating, and slicing lists. Slicing is one of Python's most powerful features.

Syntax

# The + operator concatenates two lists and returns a new list.
combined = list1 + list2

# The * operator repeats a list and returns a new list.
repeated = list * count

# A slice returns a sub-list (the stop index is not included).
sub = list[start:stop]
sub = list[start:stop:step]

# You can also assign to or delete from a slice.
list[start:stop] = new_values

Overview

SyntaxDescription
list1 + list2Returns a new list that is the concatenation of the two lists. The original lists are not modified.
list * nReturns a new list with the elements of the list repeated n times.
list[start:stop]Returns a sub-list from index start up to (but not including) stop. Negative indices are supported.
list[start:stop:step]Returns elements from start up to stop with a step interval. Use step=-1 to get a reversed copy.
list[start:stop] = valuesReplaces the slice range with another list. The original list is modified in place.

Sample Code

# Concatenate two lists with the + operator.
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)  # [1, 2, 3, 4, 5, 6]
print(a)  # [1, 2, 3] (the original list is unchanged)

# Repeat a list with the * operator.
zeros = [0] * 5
print(zeros)  # [0, 0, 0, 0, 0]

pattern = [1, 2, 3] * 3
print(pattern)  # [1, 2, 3, 1, 2, 3, 1, 2, 3]

# Basic slicing.
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums[2:5])   # [2, 3, 4] (indices 2 through 4)
print(nums[:3])    # [0, 1, 2] (first three elements)
print(nums[7:])    # [7, 8, 9] (from index 7 onward)
print(nums[:])     # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (full copy)

# Using negative indices.
print(nums[-3:])   # [7, 8, 9] (last three elements)
print(nums[:-2])   # [0, 1, 2, 3, 4, 5, 6, 7] (all but the last two)

# Slicing with a step.
print(nums[::2])   # [0, 2, 4, 6, 8] (every other element)
print(nums[1::2])  # [1, 3, 5, 7, 9] (every other element starting at 1)
print(nums[::-1])  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (reversed)

# Create a reversed copy of a list using a slice.
original = ["a", "b", "c", "d"]
reversed_copy = original[::-1]
print(reversed_copy)  # ['d', 'c', 'b', 'a']
print(original)       # ['a', 'b', 'c', 'd'] (unchanged)

# Replace elements by assigning to a slice (modifies the original list).
nums2 = [1, 2, 3, 4, 5]
nums2[1:3] = [20, 30, 40]  # Replace indices 1 and 2 with three elements.
print(nums2)  # [1, 20, 30, 40, 4, 5]

# Delete elements using a slice.
nums2[2:4] = []
print(nums2)  # [1, 20, 4, 5]

Details

Slicing is an extremely convenient Python feature that works not only on lists but also on strings, tuples, and range objects. A slice always returns a new object, so the original list is never modified.

When creating a repeated list with list * n, if the elements are themselves lists (mutable objects), the repetition creates references to the same object. For example, [[0]*3]*3 produces a 3×3 matrix where every row points to the same list, so modifying one row affects all rows. To create an independent matrix, use a list comprehension: [[0]*3 for _ in range(3)].

When assigning to a slice (list[start:stop] = values), the replacement does not need to have the same number of elements as the range being removed — the list size is adjusted automatically. To copy a list, you can use list.copy() or list[:].

If you find any errors or copyright issues, please .