Python3 List
# Python3.x Python3 Lists
Sequences are the most fundamental data structures in Python.
Each value in a sequence has a corresponding position value, called an index. The first index is 0, the second index is 1, and so on.
Python has 6 built-in sequence types, but the most common are lists and tuples.
Operations that can be performed on lists include indexing, slicing, concatenation, repetition, and membership checking.
Additionally, Python has built-in methods to determine the length of a sequence and to find the maximum and minimum elements.
Lists are the most commonly used Python data type. They can be represented as comma-separated values within square brackets.
The data items in a list do not need to be of the same type.
To create a list, simply enclose comma-separated different data items in square brackets, as shown below:
list1 =['Google','',1997,2000]
list2 =[1,2,3,4,5]
list3 =["a","b","c","d"]
list4 =['red','green','blue','yellow','white','black']
* * *
## Accessing List Values
Similar to string indexing, list indexing starts from 0, the second index is 1, and so on.
Lists can be sliced, concatenated, etc., using their indices.
!(#)
## Example
#!/usr/bin/python3
list=['red','green','blue','yellow','white','black']
print(list)
print(list)
print(list)
The output of the above example is:
red green blue
Indexing can also start from the end. The index of the last element is -1, the one before that is -2, and so on.
!(#)
## Example
#!/usr/bin/python3
list=['red','green','blue','yellow','white','black']
print(list)
print(list)
print(list)
The output of the above example is:
black white yellow
You can also use square brackets [] for slicing to access values in the list, as shown below:
!(#)
## Example
#!/usr/bin/python3
nums =[10,20,30,40,50,60,70,80,90]
print(nums[0:4])
The output of the above example is:
[10, 20, 30, 40]
Slicing using negative index values:
## Example
#!/usr/bin/python3
list=['Google','',"Zhihu","Taobao","Wiki"]
# Read the second element
print("list: ",list)
# Slice from the second element (inclusive) to the second-to-last element (exclusive)
print("list[1:-2]: ",list[1:-2])
The output of the above example is:
list: list[1:-2]: ['', 'Zhihu']
* * *
## Updating Lists
You can modify or update the data items in a list. You can also use the append() method to add list items, as shown below:
## Example (Python 3.0+)
#!/usr/bin/python3
list=['Google','',1997,2000]
print("The third element is: ",list)
list=2001
print("The updated third element is: ",list)
list1 =['Google','','Taobao']
list1.append('Baidu')
print("Updated list: ", list1)
**Note:** We will discuss the usage of the [append()](#) method in the following chapters.
The output of the above example is:
The third element is: 1997
The updated third element is: 2001
Updated list: ['Google', '', 'Taobao', 'Baidu']
* * *
## Deleting List Elements
You can use the del statement to delete elements from a list, as shown in the following example:
## Example (Python 3.0+)
#!/usr/bin/python3
list=['Google','',1997,2000]
print("Original list: ",list)
del list
YouTip