Python List
A List is a collection of data, which store the collection of data in a single variable and provide various methods for data manipulation. such as sorting, update, insert, delete, etc. This section of the tutorial you will learn about Python List.
Objectives
-
Python List Syntax
-
Create and assign value in List
-
Python List Access elements
-
List data manipulations
-
Python List all methods and it’s task
Python List Syntax
list_data = ["value", "other value"]
- Here
-
-
Values should be comma-separated into the square brackets.
-
Allow Duplicates
-
Create and assign value in Python List
# Define a list
mix_list = []
# Assign value to a list
mix_list = ["Touhid Mia", 28, True, 28.5]
Python List Access elements
This part you are going to learn access list elements using various way, let’s jump into the codes.
Common List
# Declare List for access data element
list_data = ["Touhid Mia", "Bangladesh", 28, 5.10, True]
Access element by index
Code
# Access element by index
name = list_data[0]
print(name)
Output
Touhid Mia
Process finished with exit code 0
Access element by negative index
Code
# Access element by negative index
last_item = list_data[-1]
print(last_item)
Output
True
Process finished with exit code 0
Note : Negative index means start from end of the list
Access multiple element by range of index
Code
# Access multiple element by range of index
multiple_item = list_data[1:3] # [start:end]
print(multiple_item)
Output
['Bangladesh', 28]
Process finished with exit code 0
Note : Range syntax is [start_index : end_index]
Access multiple element by range of index (without end)
Code
# Access multiple element by range of index (without end)
multiple_item = list_data[1:]
print(multiple_item)
Output
['Bangladesh', 28, 5.1, True]
Process finished with exit code 0
Note : You know that, range syntax is [start_index : end_index] , if end_index not specified it will pick value start_index to last index
Access multiple element by range of index (without start)
Code
# Access multiple element by range of index (without start)
multiple_item = list_data[:3]
print(multiple_item)
Output
['Touhid Mia', 'Bangladesh', 28]
Process finished with exit code 0
Note : If you not specify start_index then it will start from beginning
Access multiple element by range of negative index
Code
# Access multiple element by range of negative index
multiple_item = list_data[-3:-1]
print(multiple_item)
Output
[28, 5.1]
Process finished with exit code 0
Check list value is exist on it
Code
# Check list value is exist on it
if "Bangladesh" in list_data:
print("Yes key exist")
else:
print("Key not exist")
Output
Yes key exist
Process finished with exit code 0
Print each element of list by loop
Code
# Declare List for access data element
list_data = ["Touhid Mia", "Bangladesh", 28, 5.10, True]
# Print each element of list by loop
for item in list_data:
print("Item : " + str(item))
Output
Item : Touhid Mia
Item : Bangladesh
Item : 28
Item : 5.1
Item : True
Process finished with exit code 0
Python List data manipulations
Earlier part you saw that how to retrieve or access element from a list, this part you will learn how to add, remove, update data to list
Common List
# Declare List for access data element
list_data = ["Touhid Mia", "Bangladesh", 28, 5.10, True]
List add item using append function
Code
# List add item using append function
list_data.append("I have a child as well")
print(list_data)
Output
['Touhid Mia', 'Bangladesh', 28, 5.1, True, 'I have a child as well']
Process finished with exit code 0
List add item into specific index using insert function
Code
# List add item into specific index using insert function
list_data.insert(5, "I have a beautiful wife")
print(list_data)
Output
['Touhid Mia', 'Bangladesh', 28, 5.1, True, 'I have a beautiful wife', 'I have a child as well']
Process finished with exit code 0
Merge 2 list
Code
# Merge 2 list
primary_color = ["Red", "Yellow", "Blue"]
secondary_color = ["Orange", "Green", "Violet"]
print(">>> Before extend Secondary color")
print(primary_color)
primary_color.extend(secondary_color)
print(">>> After extend Secondary color")
print(primary_color)
Output
>>> Before extend Secondary color
['Red', 'Yellow', 'Blue']
>>> After extend Secondary color
['Red', 'Yellow', 'Blue', 'Orange', 'Green', 'Violet']
Process finished with exit code 0
Update list item using index
Code
# Update list item using index
list_data[0] = "Mia Bhai"
print(list_data)
Output
['Name', 'Country', 'Bangladesh', 28, 5.1, True, 'I have a beautiful wife', 'I have a child as well']
Process finished with exit code 0
Update list multiple item using index
Code
# Update list multiple item using index
list_data[0:1] = ["Name", "Country"]
print(list_data)
Output
['Name', 'Country', 'Bangladesh', 28, 5.1, True, 'I have a beautiful wife', 'I have a child as well']
Process finished with exit code 0
Remove List Item using name
Code
# Existing List
list_data = ["Touhid Mia", "Bangladesh", 28, 5.10, True]
# Remove List Item using name
list_data.remove("Bangladesh")
print(list_data)
Output
['Touhid Mia', 28, 5.1, True]
Process finished with exit code 0
Remove List Item using Specified Index by pop function
Code
# Existing List
list_data = ["Touhid Mia", "Bangladesh", 28, 5.10, True]
# Remove List Item using Specified Index by pop function
list_data.pop(0)
print(list_data)
Output
['Bangladesh', 28, 5.1, True]
Process finished with exit code 0
Remove List last item by pop function
Code
# Existing List
list_data = ["Touhid Mia", "Bangladesh", 28, 5.10, True]
# Remove List last item by pop function
list_data.pop()
print(list_data)
Output
['Touhid Mia', 'Bangladesh', 28, 5.1]
Process finished with exit code 0
Remove list all item using clear function
Code
# Existing List
list_data = ["Touhid Mia", "Bangladesh", 28, 5.10, True]
# Remove list all item using clear function
list_data.clear()
print(list_data)
Output
[]
Process finished with exit code 0
Python List all methods and it’s task
-
append() : Adds an element at the end of the list
-
count() : Returns the number of elements with the specified value
-
index() : Returns the index of the first element with the specified value
-
pop() : Removes the element at the specified position
-
copy() : Returns a copy of the list
-
insert() : Adds an element at the specified position
-
remove() : Removes the item with the specified value
-
extend() : Add the elements of a list (or any iterable), to the end of the current list
-
clear() : Removes all the elements from the list
-
reverse() : Reverses the order of the list
-
sort() : Sorts the list