In Python, List is an important and powerful data structure type that allows you to store an ordered and mutable number of variables in a collection. Lists have properties and functions that can be useful in solving many problems and applications.
List properties in Python
Lists in Python have a number of key features. These features are as follows. Lists can store an unlimited number of variables. In other words, you can put an infinite number of elements in a list. Each element in the list is numbered with an index. The indices start from zero, which means that the first element of the list has index 0, the second element has index 1 and so on. This feature allows you to directly access the elements of the list. Lists can contain any type of data. That is, you can store numbers, strings, objects, other lists and even a combination of them in a list. One of the main features of the list in Python is its mutability. This means that you can easily change, add, remove or move the list elements. Thus, a list in Python is a mutable data structure. Python has a variety of functions that you can apply to lists. These functions include adding, removing, counting elements, searching in a list, sorting, etc. These functions allow you to manage and modify lists effectively. Using these features and functions, you can use lists extensively in Python programming and easily work with the data and elements inside the lists.
Creating a list in Python
The process of creating a list in Python is not complicated. The following code snippet shows how to define a list in Python.
# Define a list
numbers = [1, 2, 3, 4, 5]
# Access list elements using index
print(numbers[0]) # Result: 1
print(numbers[2]) # Result: 3
# Change the value of a list element
numbers[1] = 10
print(numbers) # result: [1, 10, 3, 4, 5]
Creating a list using distinct and repeated elements
In Python, we can construct a list using distinct and repeated elements. As we mentioned, to make a list with distinct elements, we can use the symbols [] and separate the elements using commas (,). It is necessary to explain that to build a list with repeated elements, we can use techniques such as repeating the element using the multiplication operator (*) or using the corresponding functions such as list. For a better understanding of the subject, pay attention to the following examples.
fruits = ['apple', 'orange', 'banana', 'grape']
numbers = [1, 2, 3, 4, 5]
Creating a list with repeated elements (element repetition using the multiplication operator):
repeated_numbers = [1] * 5 # Repeat the number 1 five times
repeated_strings = ['Hello'] * 3 # Repeat the string "Hello" three times
Creating a list with repeated elements (using the list function):
repeated_elements = list(range(5)) # Repeat numbers 0 to 4
In the examples above, you can create lists with distinct or duplicate elements. According to your need, you can determine the elements of the list and use different techniques to build the list.
Access to elements in the list
In Python, you can access the elements in a list using indices. Indexes in lists start from zero, which means that the first element of the list has index 0, the second element has index 1, and so on. To access an element in the list, you can use the following syntax:
my_list[index]
Here my_list is the name of the list and index is the index of the desired element. For example, if we want to access the first element of the list, its index will be 0. So, using my_list[0] we can access the first element of the list. Here are some code examples to access the elements in the list:
my_list = ['apple', 'orange', 'banana', 'grape']
print(my_list[0]) # access the first element - output: 'apple'
print(my_list[2]) # access the third element - output: 'banana'
print(my_list[-1]) # access last element - output: 'grape'
Also, you can use negative subscript. A negative index allows you to access elements from the end to the beginning. For example, my_list[-1] returns us the last element of the list. If you want to access elements inside nested lists, you can combine multiple index levels using [ ]. for example:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(nested_list[0][1]) # access to the second element in the first list - output: 2
print(nested_list[2][0]) # access to the first element in the third list - output: 7
In this example, nested_list[0][1] returns the second element in the first list, which is equal to 2.
Negative indices
Negative indices in Python allow you to access the elements of a list from the end to the beginning. For example, if we denote the last element of the list with index -1, we will return the penultimate element with index -2 and so on. Below are some code examples for using negative indices in accessing list elements:
my_list = ['apple', 'orange', 'banana', 'grape']
print(my_list[-1])
print(my_list[-2])
print(my_list[-3])
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(nested_list[-1][-2]) # access the second element from the end in the last list - output: 8
print(nested_list[-3][-1]) # access to the first element from the end in the first list - output: 3
The negative index allows you to access the elements of the list in reverse order. So, my_list[-1] returns the last element of the list, my_list[-2] returns the penultimate element, and so on until the first element. In this example, nested_list[-1][-2] returns the second element from the end in the last list, which is equal to 8.
Finding the size of lists in Python
In Python, you can use the len function to find the size (number of elements) of a list. The len function returns the number of elements in the list as output. The following code snippet shows how to do this.
my_list = ['apple', 'orange', 'banana', 'grape']
list_size = len(my_list)
print(list_size) # Output: 4
In this example, len(my_list) returns the number of elements in my_list and stores it in the list_size variable. Then, using the print function, we print the value of list_size, the output of which is 4. If the lists are nested, you can use loop functions (such as a for loop) to recursively count the number of elements in the lists. for example:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
total_size = 0
for sublist in nested_list:
total_size += len(sublist)
print(total_size)
In this example, we have a nested list called nested_list. Using a for loop, for each nested list in nested_list, we calculate its size using the len function and add it to the total_size variable. Finally, the total_size value, which is the sum of the elements of the internal lists, is printed. In this case, the output is 9.
Getting input as a list in Python
In Python, you can get inputs as lists. One method for this is the input function, which receives input from the user, and the split function, which splits the input into list elements. The input function receives a string from standard input, usually the console. It then splits the string into list elements based on spaces (or any other characters you specify) using the split function. The following code snippet shows how to do this.
input_string = input("Please separate list elements with spaces: ")
input_list = input_string.split()
print(input_list)
In this example, we first enter a string using the input function and store it in the input_string variable. Then, using the split function, we split the string based on space and store the result in the input_list variable. Finally, we print the value of input_list or a list of elements entered by the user. For example, if the user enters the input "apple orange banana", the output will be ['apple', 'orange', 'banana'].
Note that in the example above, split is called without input. In this case, space is used as the delimiter character. If you want to split based on another character, you can give it as input to split. For example, split(',') is used to split based on commas.
Add element to list in Python
In Python, you can use different methods to add elements to a list. Among the common methods, the following should be mentioned.
Using the append method: You can use the append method on a list to add a new element to the end of the list. The append method receives the new element as a parameter and attaches the element to the end of the list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
Using the "+" operator to connect two lists: You can use the "+" operator to connect two lists and get a new list.
my_list = [1, 2, 3]
new_element = 4
new_list = my_list + [new_element]
print(new_list) # Output: [1, 2, 3, 4]
Using the insert method: You can use the insert method on a list to place the new element in a specific position in the list. The insert method receives two parameters; desired position and new element.
my_list = [1, 2, 3]
new_element = 4
my_list.insert(1, new_element)
print(my_list) # Output: [1, 4, 2, 3]
In the examples above, we first defined an initial list and then used various methods to add new elements. Finally, we printed the result separately.
One dimensional list
A one-dimensional list in Python stores an array of elements. List elements can be any data type (numbers, strings, objects, etc.). To define a list, square brackets [] are used.
# Define an empty list
my_list = []
# Define a list with elements
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "orange", "banana"]
mixed = [10, "string", True]
# Access list elements using index
print(numbers[0]) # Result: 1
print(fruits[1]) # Result: Orange
# Change the value of a list element
mixed[0] = 20
print(mixed) # result: [20, "string", True]
Multidimensional list
Multidimensional lists in Python are collections of lists. Thus, each element of the main list can be another list. In this case, multiple indices are used to access the elements of the multidimensional list.
# Define a multidimensional list
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Access to multidimensional list elements
print(matrix[0]) # result: [1, 2, 3]
print(matrix[1][2]) # Result: 6
# Change the value of a multidimensional list element
matrix[2][1] = 10
print(matrix)
# Result: [[1, 2, 3], [4, 5, 6], [7, 10, 9]]