In this article List in Python we give the information about list is a serialized data structure that can store different types of data. Here are some examples of creating and using a list:
How to create a list in Python
# Creating an empty list
empty_list = []
# Creating a list with some items
list_ofnumbers = [1, 2, 3, 4, 5]
fruits_list = [“apple”,”banana”,”grapes”]
# Creating a mixed type list
mixed_list = [“Hello”, 100, 3.14, True]
# to print
print(“Empty list:”, empty_list)
print(“List of numbers:”, list_of_numbers)
print(“Fruits list:”, fruits_list)
print(“Mixed list:”, mixed_list)
Adding Items to List
# new list
flowers_list = []
#adding flowers
flower_list.append(“rose”)
flower_list.append(“lotus”)
flower_list.append(“marigold”)
print(“List of flowers:”, list_of_flowers)
Remove Items from List
#Pre-made list
fruits_list = [“apple”,”banana”,”grapes”,”orange”]
# removing an element
fruits_list.remove(“banana”)
print(“Updated Fruits List:”, Fruits_List)
Other useful list operations
- Find Length
print(“Length of list:”, len(list_of_fruits))
Sort Elements
list_ofnumbers = [5, 3, 1, 4, 2]
list_of_numbers.sort()
print(“List of sorted numbers:”, list_of_numbers)
Reverse List
list_of_numbers.reverse()
print(“List of reverse numbers:”, list_of_numbers)
- Accessing elements of the list
print(“First fruit:”, fruit_list[0])
print(“Last fruit:”, fruit_list[-1])
Use List in Python:
- It is useful for combining different types of data.
- It is mutable.
- It features inbuilt operations like adding, deleting, sorting.
Create a List:
List is a data structure in Python that can store different types of data (like numbers, strings, etc.).
Example:
# empty list
my_list = []
# List with different types of data
my_list = [1, 2, 3, “Python”, True]
print(my_list) # Output: [1, 2, 3, ‘Python’, True]
- Getting and Set Items from the List:
Any item in the list can be accessed or changed using indexing.
The index starts from 0.
Getting Items:
my_list = [10, 20, 30, 40]
print(my_list[1]) # output: 20
Set Items:
my_list[2] = 50
print(my_list) # Output: [10, 20, 50, 40]
- Adding and Removing Items:
Adding Items:
- append(): Adds item to the end of the list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
- insert(): Adds item at a particular location (index).
my_list.insert(1, 10)
print(my_list) # Output: [1, 10, 2, 3]
Remove Items:
- remove(): Removes the item with the given value.
my_list = [1, 2, 3, 4]
my_list.remove(3)
print(my_list) # Output: [1, 2, 4]
pop(): Removes the item at a particular index. If index is not given, removes the last item.
my_list = [1, 2, 3, 4]
my_list.pop(1)
print(my_list) # Output: [1, 3, 4]
clear(): Clears the list completely.
my_list.clear()
print(my_list) # Output: []
List Slices:
To get a slice of the list, use :.
my_list = [10, 20, 30, 40, 50]
# Slice from 2nd to 4th item (index 1 to 3)
print(my_list[1:4]) # Output: [20, 30, 40]
# from beginning to third
print(my_list[:3]) # Output: [10, 20, 30]
# third to last
print(my_list[2:]) # Output: [30, 40, 50]
- Different List Methods:
len(): Returns the length of the list.
my_list = [1, 2, 3, 4]
print(len(my_list)) # Output: 4
sort(): Sorts the list.
my_list = [4, 2, 1, 3]
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4]
reverse(): Reverses the list.
my_list = [1, 2, 3]
my_list.reverse()
print(my_list) # Output: [3, 2, 1]
index(): Returns the index of a value.
my_list = [10, 20, 30, 40]
print(my_list.index(30)) # Output: 2
count(): Returns the frequency of a value.
my_list = [1, 2, 2, 3, 4]
print(my_list.count(2)) # Output: 2
copy(): Makes a copy of the list.
original_list = [1, 2, 3]
copied_list = original_list.copy()
print(copied_list) # Output: [1, 2, 3]
Conclusion:
List is a very useful and flexible data type in Python. Using these methods you can perform various operations with lists.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining