In Python, a list is a data type that can store a collection of items. It is similar to an array in other languages but has some unique features.
A python list is enclosed with square brackets([]
) and each item is separated by a comma. For example,
a_list = [9,3,2,4,5]
This list contains five items: The integers are 9, 3, 2, 4, and 5.
Lists in Python are very flexible and can hold items of different data types. For example, the following list contains an integer, string, float, and bool:
a_list = [9, "hello", 0.4, True]
Accessing items in a list:
To access items in a list, you can use indexing. In Python, indexing starts at 0, so the first item in the list has an index of 0, the second item has an index of 1, and so on. For example:
a_list = [9, "hello", 0.4, True]
# first index
a_list[0] # it will print 9
# second index
a_list[1] # it will print "hello"
Indexing
Index in Python are two ways to access, one is positive indexing and another one is negative indexing. For example,
# from last list
a_list
# output
[9, "hello", 0.4, True]
# positive index
a_list[2]
# output
0.4 # it is left to right and start with 0
# negative index
a_list[-1]
# output
True # it is right to left and start with -1
# negative index
a_list[-3]
# output
"hello"
Python list Methods
There are many built-in methods that you can use to work with lists. Some of the most commonly used methods are:
append()
: Adds an element to the end of the listclear()
: Removes all elements from the listcopy()
: Makes a copy of the listcount()
: Counts the number of times an element appears in the listextend()
: Adds the elements of a list (or any iterable) to the end of the current listindex()
: Returns the index of the first occurrence of an element in the listinsert()
: Inserts an element at a specific index in the list. Syntaxlist.insert(index,element)
pop()
: Removes an element at a specific index in the list (or the last element if no index is specified).remove()
: Removes the first occurrence of a specific element from the listreverse()
: Reverses the order of the listsort()
: Sorts the elements of the list in place (in ascending order by default)
These are just a few examples of the many built-in methods that are available for working with lists in Python. You can find a complete list of methods in the Python documentation.
For example,
my_list = [1,2,3,4]
# append
my_list.append(5) # return [1, 2, 3, 4, 5]
# clear
my_list.clear() # return []
list.copy() method
In python, The copy()
method is used dynamically. it is used for duplicating the value or data to modify that data without affecting the original data.
For example,
a_list = [3, 4, 5, 6]
anthoer_list = lst.copy() # duplicate the a_list list
# we can change any list does not affect remaining list
anthoer_list.append(100)
# changing original list
a_list.append(7)
a_list.append(8)
# result
print(a_list) # return [3, 4, 5, 6, 7, 8]
print(anthoer_list) # return [3, 4, 5, 6, 100]
Here results of both lists have differed.
Suppose, we can't use copy()
the method. Both lists have been affected and values are also the same in the end.
b_list = ['first', 'second']
c_list = b_list # assign both list value is same
c_list.append('three')
b_list.append('four')
print(c_list) # return ['first', 'second', 'three', 'four']
print(b_list) # return ['first', 'second', 'three', 'four']
How list works in loops
Python has two types of loops:
for loop
while loop
For loop in the python list
In python, a for loop is used to iterate over a sequence of elements, such as a list or a string. Here is an example of a for loop that iterates over a list of numbers and prints each one to the console:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
In this example, the for loop will iterate over each element in the numbers
list. On each iteration, the current element will be assigned to the num
variable, and the code within the loop will be executed. This code simply prints the value of num
to the console. The output of this code will be:
1
2
3
4
5
For loops are a very powerful tool in Python and are used quite frequently in many programs. They are particularly useful for performing some action on each element in a list or other sequence.
While loop in the python list
A while loop in Python is a type of loop that continues to execute a block of code as long as a certain condition remains True. Here is an example of a while loop in Python:
i = 0
while i < 5:
print(i)
i += 1
In this example, the while loop will continue to execute as long as the value of the i
variable is less than 5. On each iteration of the loop, the code within the loop will be executed, which in this case is simply printing the value of i
to the console. Then, the i
variable will be incremented by 1 using the i += 1
statement. This will cause the loop to eventually terminate once the value of i
reaches 5, at which point the condition i < 5
will be False. The output of this code will be:
0
1
2
3
4
While loops are useful when you want to keep executing a block of code as long as a certain condition remains True. However, you must be careful to ensure that the condition will eventually become False, or else the loop will continue to run indefinitely, which is often called an "infinite loop".
Now you can understand the python list fundamentals. The upcoming tutorial we can learn:
Python List Comprehensions
Python List Operations
Thank you.