Operations with lists: concatenation, duplication, deletion, slice, access by index

Operations with lists: concatenation, duplication, deletion, slice, access by index

1. Basic operations with lists

The following basic operations are used to work with lists:

  • + – concatenation of lists;
  • * – duplication of the list;
  • len – determine the length of the list;
  • del – remove an item from the list;
  • assignment by index;
  • using the slice to access the list items and change the list.

 

2. Example of concatenation (+) of a list

The example demonstrates the operation of adding (concatenating) lists

# List L
L=[2,3,4]

# List LS
LS = ["456", 7, 3.1415]

# add to the list L list LS
L=L+LS # L = [2, 3, 4, '456', 7, 3.1415]
print("L = ",L)

As a result of executing the above code, the following result will be displayed.

L = [2, 3, 4, '456', 7, 3.1415]

 

3. An example of duplicating the list. Operation *

The example demonstrates the operation of duplicating a list, which is denoted by the symbol *. The text of the program that demonstrates the operation of duplicating lists is as follows:

# Duplicating the lists
A = [ 1, 3, 'abcde', -0.02 ] # specified list
B = A # copying list A to list B
C = A*3 # duplication of list A to list C 3 times (3 copies)

print("A = ", A)
print("B = ", B)
print("C = ", C)

In the above program code, list A is first created. This list is then copied to list B using the assignment operator =. In line

C = A*3

list A is duplicated in list C 3 times.

The above code will give the following result.

A =   [1, 3, 'abcde', -0.02]
B =   [1, 3, 'abcde', -0.02]
C =   [1, 3, 'abcde', -0.02, 1, 3, 'abcde', -0.02, 1, 3, 'abcde', -0.02]

 

4. Removing items from the list. Operation del
4.1. An example of removing a single item from the list

The del operation removes the specified item or range of items from the list. In the following example, the following list operations are performed:

  • a list A is created;
  • list B is created from list A;
  • item with index 2 is removed from list B.

The program code for using the operation del

# Operation del
# Create a list
A = [ 1, 2, 3, 4, 5, 6 ]

# Create a new list
B = A[0:5] # B = [1, 2, 3, 4, 5]

# remove item with index 2 from list B
del B[2]

print("A = ", A)
print("B = ", B)

Executing the above code gives the following result

A = [1, 2, 3, 4, 5, 6]
B = [1, 2, 4, 5]

It is important that the correct index of the element be removed from the list. If the index is incorrect, the Python interpreter will display an error message. For example, if you specify

del B[7] # error in the index

the interpreter will display the following message

IndexError: list assignment index out of range

 

4.2. Example of operation del L[i:j] which remove a range of elements from the list

The del operation allows you to remove a range of items from the list. In this case, the first and next elements of the range to be deleted are indicated by the symbol : (colon).

The following example demonstrates the use of the del operation for ranges of numbers:

# Del operation - delete range
# Create a List A
A = [ 'a', 'b', 'c', 'd', 'e', 'f' ]

# Create a new list B
B = list(A)

# remove characters 'b' and 'c' from list B
del B[1:3] # B = ['a', 'd', 'e', 'f']

# Form another list C from list A
C = list(A) # C = [ 'a', 'b', 'c', 'd', 'e', 'f' ]

# remove from the list C the last 3 items
n = len(C)
del C[n-3:n] # C = ['a', 'b', 'c']

# form another list D
D = list(A)

# remove from the list D the last element with the checking for correctness
n = len(D)
if n>0:
    del D[n-1]

# display the result
print("A = ", A)
print("B = ", B)
print("C = ", C)
print("D = ", D)

Running the above code will produce the following result.

A =   ['a', 'b', 'c', 'd', 'e', 'f']
B = ['a', 'd', 'e', 'f']
C = ['a', 'b', 'c']
D = ['a', 'b', 'c', 'd', 'e']

 

5. The operation of assigning a list item at index L[i]. Example

Python has the ability to access a specific list item by its index using square brackets [ ]. Indexes are numbered from zero.

Index values can be both negative and positive. If the values of the indexes are negative, then the offset of the indices occurs from right to left. If the values of the indexes are positive, then the offset of the indices occurs from left to right.

Example. The example demonstrates how to access individual list items.

# The operation of assignment of individual elements of the list
# Create a list A
A = list(range(1,10)) # A = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# call the assignment operation
t1 = A[-1] # t=9 - negative indexes are scanned from right to left
t2 = A[3] # t=4 - positive indexes are scanned from left to right
t3 = A[0] # t3=1

print("t1 = ",t1)
print("t2 = ",t2)
print("t3 = ",t3)

Executing the above code gives the following result

t1 = 9
t2 = 4
t3 = 1

 

6. The operation of assigning multiple items to the list using the slice L[i:j]

When working with lists in Python, it is possible to assign several elements. Such an assignment has the name “slice assignment”. As a result, a new list is created that contains several elements that are specified by a range.

Example of using the assignment operation

# Features of the operation of assigning a slice
# Create a List A
A = [ 'abc', 'def', 'ghi', 23.5, 8.8, -11 ]

# Create a list B, which contains 2,3,4 elements of list A
B = A[1:4] # B = ['def', 'ghi', 23.5]

# Create a C list that contains the last two list items of list A
n = len(A)
C = A[n-2:n] # C = [8.8, -11]

print("A = ",A)
print("B = ",B)
print("C = ",C)

The result of the program

A =   ['abc', 'def', 'ghi', 23.5, 8.8, -11]
B = ['def', 'ghi', 23.5]
C = [8.8, -11]

 

7. The operation of changing the list using the slice L[i:j]

Using the slice, you can change an existing list. The operation of changing the list with a slice is performed in 2 steps:

  1. The part of the list that is located to the left of the operator = is removed.
  2. An insertion in the list of new objects placed to the right of the = operator is performed. The insertion is carried out starting from the left edge where the previous deleted slice was located.
# Modifying a list with a slice
# The list is given
A = [0,0,0,0,0,0]

# Change the list using the slice
B = list(A) # make a copy of the list A, B = [0,0,0,0,0,0]
B[1:]=[1,1,1] # B = [0, 1, 1, 1]

C = list(A)   # C = [0,0,0,0,0,0]
C[1:3]=[1,1,1] # C = [0,1,1,1,0,0,0]

D = list(A)   # D = [0,0,0,0,0,0]
D[:3]=[1,1,1] # D = [1,1,1,0,0,0]

E = list(A)
E[:5]=[1,1,1] # E = [1,1,1,0]

F = list(A)
F[1:10]=[1,1,1] #F = [0,1,1,1]

print("A       = ",A)
print("B[1:]   = ",B)
print("C[1:3] = ",C)
print("D[:3]   = ",D)
print("E[:5]   = ",E)
print("F[1:10] = ",F)

The result of the program

A       =   [0, 0, 0, 0, 0, 0]
B[1:]   =   [0, 1, 1, 1]
C[1:3]  =   [0, 1, 1, 1, 0, 0, 0]
D[:3]   =   [1, 1, 1, 0, 0, 0]
E[:5]   =   [1, 1, 1, 0]
F[1:10] =   [0, 1, 1, 1]