Python list is a mutable, ordered, and iterable collection of elements. Lists can contain items of different data types, such as integers, floats, strings, and even other lists or custom objects. It is a collection of values enclosed in [ ] and separated by commas.
How to Create a List
You can create a list by enclosing a comma-separated sequence of items within square brackets [].
# Creating a list with integers
numbers = [1, 2, 3, 4, 5]
# Creating a list with mixed data types
mixed_list = [1, "hello", 3.14, [1, 2, 3]]
print(mixed_list)
Output
[1, 'hello', 3.14, [1, 2, 3]]
How to access list elements
You can access list elements by their index, starting from 0 for the first item using the square brackets [].
# Creating a list with integers
numbers = [1, 2, 3, 4, 5]
# Creating a list with mixed data types
mixed_list = [1, "hello", 3.14, [1, 2, 3]]
print(mixed_list[0])
print(mixed_list[1])
Output
1
hello
Modify list elements using their index
numbers = [1, 2, 3, 4, 5]
numbers[0] = 11
print(numbers)
Output
[11, 2, 3, 4, 5]
Adding items to the list using the append() method or the + operator
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers)
Output
[11, 2, 3, 4, 5]
Removing elements from the list
You can remove elements from the list using the remove() method, del statement, or pop() method.
numbers = [1, 2, 3, 4, 5]
numbers.remove(2)
del numbers[0]
popped_item = numbers.pop()
print(numbers)
print(popped_item)
Output
[3, 4]
5
Use list comprehensions to create new lists based on existing lists
numbers = [1, 2, 3, 4, 5]
squares = [number**2 for number in numbers]
print(squares)
Output
[1, 4, 9, 16, 25]
Lists in Python are flexible and can be used for various purposes, such as storing and processing data, organizing objects, and implementing data structures like stacks and queues.
Check if an Element Exists in a List
You can use the “in” keyword to check if an item exists in the list or not.
dbs = ['Data', 'Base', 'Firebase']
print('D' in dbs)
print('Firebase' in dbs)
Output
False
True
Python List Length
You can use the “len()” function to find the size of a list. For example,
dbs = ['Data', 'Base', 'Firebase']
print(len(dbs))
Output
3
List Methods
Method | Description |
---|---|
append() | It adds an item to the end of the list |
extend() | It adds all the items of an iterable to the end of the list |
insert() | It inserts an item at the specified index |
remove() | It removes items present at the given index |
pop() | It returns and removes items present at the given index |
clear() | It removes all items from the list |
index() | It returns the index of the first matched item |
count() | It returns the count of the specified item in the list |
sort() | It sorts the list in ascending/descending order |
reverse() | It reverses the item on the list |
copy() | It returns the shallow copy of the list |
That’s it!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.