Python. Lists. List properties. Examples that demonstrate different properties of lists

Lists. List properties. Examples that demonstrate different properties of lists. Operations list, len


Contents



1. What is a list in the Python programming language? Features of using lists in Python

In the Python programming language, a list is an object type that allows you to contain objects of various types: numbers, strings, lists, etc. In other words, the list is an entry that combines objects of various types (numbers, strings, etc.).

Lists are effective for programming tasks in which you need to store different types of data.

Compared to other programming languages in Python, working with lists is very convenient. To implement correct work with lists in another, lower-level programming language (for example, C), you need to put a lot of effort. In Python, lists implement everything you need to handle collections of data.

Lists are objects that can be directly modified using an assignment operation.

 

2. Main properties of lists

There are following properties of lists:

  1. Lists are sequences of objects of arbitrary types. Lists are groups of objects of arbitrary types that provide positional sequential placement of elements in the order “from left to right”.
  2. Providing access to the list item by index. In Python, it is possible to get a list item using an indexing operation (see p. 5). In this case, the list item with the specified index is obtained. The index numbers in the list begin with 0. In addition, you can receive elements using slices and concatenation of lists.
  3. Variable length of lists. The number of items (objects) in the list can be changed. In other words, the lists can be increased and decreased (see p. 6).
  4. Heterogeneity of lists. The term “heterogeneity of lists” means that lists may contain other complex objects (see p. 8).
  5. Arbitrary number of nested lists. Lists can be nested. That is, you can create lists from lists (see p. 9).
  6. Lists are categorized as mutable objects. This property means that lists can be modified directly. Lists support operations that allow you to change them, namely: assignment by index, adding/deleting elements by index, concatenation, assignment to slice.
  7. Ability to contain arrays of references to other objects. Lists may contain zero or more references to other objects (see p. 8). Lists can contain a reference to an object (objects) in the case that this object is assigned to a name of a variable or structure.

 

3. Examples of creating lists. Creating an empty list

Example 1. The example shows how to create a list using an assignment operator, a while loop operator, and a concatenation operator +.

# Examples of creating lists in various ways
# Creating an empty list
L1 = []

# Creating the list by an operation of concatenation +
i=0
while i<10:
    L1=L1+[i]
    i=i+1
print("L1 = ",L1)

As can be seen from the program code, in the line

L1 = []

an empty list is created

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

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

Example 2. Creates a list of characters from an object of type “string”. The string consists of characters. These characters can create a sequence, that is, a list.

# Examples of creating lists in various ways
# Creating a list from an object to be iterated
L = list('abc') # L = [ 'a', 'b', 'c']
print("L = ",L)

In this example, creating a string

L = list('abc')

you can write on another

L = list("abc")

The result of the program

L =   ['a', 'b', 'c']

Example 3. A list of continuous sequences of integers is created

# Examples of creating lists in various ways
# Creating a list from a continuous sequence of integers
L = list(range(1,10))
print("L = ",L) # L = [1, 2, 3, 4, 5, 6, 7, 8, 9]

As can be seen from the code, to obtain the sequence 1, 2, …, 9, use the operation

range(1,10)

To create a list, use the operation

list(range(1,10))

The word list means the data type name “list” and is used to create lists.

The result of the program

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

 

4. What is the purpose of the list operation?

The list operation is used to create a new copy of the list. The list operation is the name of the list data type.

In Python, the list operation is implemented as a separate class that contains a large set of methods for working with lists. To view the list of list class methods in the Python Shell, type

help(list)

For more information on using the methods of the list class, see the topic:

  • Methods of working with lists. Examples

Below are examples of creating lists using the list operation.

DAYS = list(range(1,8)) # DAYS = [1, 2, 3, 4, 5, 6, 7]
SYMBOLS = list("Hello") # ITEMS = ['H', 'e', 'l', 'l', 'o']

 

5. How to access a list item by its index? Examples

Example 1. The example creates a list of 3 items of different types. Then, in turn, the values of each item in the list are displayed.

>>> myList=[2.5, 8, "Hello"]
>>> myList[0] # display list item by index 0
2.5
>>> myList[1]
8
>>> myList[2]
'Hello'
>>>

As can be seen from the example, the numbering of the elements of the list starts from 0.

Example 2. Using a list item in an expression. It is important that the value of the list item is correct in the operations that are used in the expression.

# use of the list in the expression
L=[2,3,4]
x=5
y=x+L[1] # y=5+3=8
print("y = ",y)

LS = ["456", 7, 3.1415]
s = "123"
s += LS[0] # s="123456"
print("s = ", s)

The result of the program

y = 8
s = 123456

If you try to add a string to the number

s = s + LS[1] # Add a string to the number - an error!

then the Python interpreter will display an error message.

TypeError: can only concatenate str (not "int") to str

 

6. An example that demonstrates the property of increasing/decreasing the length of the list

The example demonstrates the property of increasing and decreasing the list A. Increasing the length of the list is performed in a loop using the concatenation + operation. Decreasing the length of the list is implemented in two ways:

  • using a while loop, in which the position of the last element is calculated. The number of elements in the loop is determined by the operation len. Deleting an item is done with the del operation;
  • using the del operation, which specifies the range of items to be deleted.

The text of the program is as follows:

# Examples of increasing / decreasing the length of the list
# create a list
A = []

# create a list of 10 items by typing
i=0
while i<10:
    x = float(input("x = "))
    A = A+[x] # list increase
    i=i+1

print("Source list ")
print("A = ",A)

print("Decreasing list A on 3 items ")
i=0
while i<3:
    n = len(A) # get the length of the list
    del A[n-1] # delete the last item in the list
    i=i+1
print("A = ", A)

print("Decreasing of the list for 2 last items ")
del A[n-3:n-1]
print("A = ", A)

The result of the program

x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
Source list
A = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
Decreasing list A on 3 items
A = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
Decreasing of the list for 2 last items
A = [1.0, 2.0, 3.0, 4.0, 5.0]

 

7. How to determine the length of the list? Operation len. Example

The length of the list is the number of items in the list. The length of the list is determined by the operation len.

Example. The example determines the length of the list using the len operation.

# Determiningthe length of the list using operation len
A = [ 3.5, 2.8, 'abc', [ 2, 3, False]]
length = len(A)
print("length = ", length) # length = 4

B = [ "Hello world!" ]
length = len(B)
print("length = ", length) # length = 1

C = [ 0, 3, 2, 4, 7 ]
length = len(C)
print("length = ", length) # length = 5

The result of the program

length = 4
length = 1
length = 5

 

8. An example of creating a list containing other complex objects

The example creates a list with the name D, which contains other complex objects:

  • two lists with the names A, B;
  • a tuple named C;
  • a string named STR.

 

# An example of a list containing complex objects
# lists, tuples, and character strings are declared
A = [] # empty list
B = [ 1, 3, -1.578, 'abc' ] # list with objects of different types
C = ( 2, 3, 8, -10 ) # tuple
S = "Hello world!"

# list containing various complex objects
D = [ A, B, C, S ]

print("D = ", D)

Program execution gives the following result

D = [[], [1, 3, -1.578, 'abc'], (2, 3, 8, -10), 'Hello world!']

 

9. An example showing the property of an arbitrary number of nested list

In the example, the list L is formed, which includes two sublists.

# Arbitrary number of nested lists
L = [ [ 'abc', 2.5, 117 ], [ 29, False, 'DEF', "Hello" ] ]

# output of list items
print("L[0][1] = ", L[0][1]) # L[0][1] = 2.5
print("L[0][2] = ", L[0][2]) # L[0][2] = 117
print("L[1][0] = ", L[1][0]) # L[1][0] = 29
print("L[1][2] = ", L[1][2]) # L[1][2] = 'DEF'

The result of the program

L[0][1] = 2.5
L[0][2] = 117
L[1][0] = 29
L[1][2] = DEF

 

10. What is the result of assigning the A=B lists?

If you perform the operation of assigning lists A = B, then these two names will point to the same list. This means that changes in list B will affect list A. Similarly, changes in list A will affect list B.

In order to create another list B from list A (a copy of list A), you need to use the list operation.

For example.

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

# invoke the assignment operation
B = A # Names B and A point to the same list

# change the list B
B[3] = 100 # list A is also changed

# Create a copy of list A using the list operation
C = list(A) # C = [1, 2, 3, 100, 5, 6, 7, 8, 9]

# change the list C
C[3] = 777; # only list C is changed, list A does not changed

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

The above program will give the following result

A = [1, 2, 3, 100, 5, 6, 7, 8, 9]
B = [1, 2, 3, 100, 5, 6, 7, 8, 9]
C = [1, 2, 3, 777, 5, 6, 7, 8, 9]

 


Related topics