Lists and methods of the list in Python
What are the lists?
A list in Python is a data structure that stores an ordered collection of items, which can be of any data type, including numbers, strings, and even other lists.
A list can grow or shrink during the execution of the program. Hence it is also known as a dynamic array.
Because of the nature of lists they are commonly used for handling variable-length data.
Lists properties
Lists can be enclosed with square bracket '[ ]' and values inside the list are separated with commas
fruits = ["apple", "banana", "cherry"]
print(fruits)
//Output
['apple', 'banana', 'cherry']
Python Lists are mutable
Lists in Python are mutable, meaning the elements within a list can be changed after the list is created.
For example:
fruits = ["apple", "banana", "cherry"]
print("Before modification: ", fruits)
fruits[1] = "orange"
print("After modification: ", fruits)
//Output
Before modification: ['apple', 'banana', 'cherry']
After modification: ['apple', 'orange', 'cherry']
A list can contain dissimilar types, they are a collection of dissimilar types.
For example:
li = ["Sanjay", 30, 2.5]
print(li)
//Output
['Sanjay', 30, 2.5]
Items in a list can be repeated, i.e., a list may contain duplicate items. Like printing, * can be used to repeat an element multiple times. An empty list is also feasible.
For example:
ages = [5, 6, 5, 7, 6] # duplicates allowed
num = [10]*5 # stores [10, 10, 10, 10, 10]
lst = [] # empty list, valid
print(ages)
print(num)
print(lst)
//Output
[5, 6, 5, 7, 6]
[10, 10, 10, 10, 10]
[]
List slicing
List slicing in Python is a technique to extract a portion of a list, creating a new list that contains only a subset of the original list's elements. Slicing is performed by specifying the start index and the end index separated by a colon ':'.
The syntax for list slicing is as follows:
list[start:end]
Where 'start' is the index of the first element to include in the slice and 'end' is the index of the first element to exclude from the slice. The slice will include all elements from '
start
'
up to but not including 'end
'
.Here are some examples of using list slicing in Python :
- Getting a slice of a list:
fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
print(fruits[1:4])
//Output
['banana', 'cherry', 'orange']
- Getting a slice from the beginning of a list:
fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
print(fruits[:3])
//Output
['apple', 'banana', 'cherry']
- Getting a slice until the end of a list:
fruits = ["apple", "banana", "cherry", "orange", 'kiwi']
print(fruits[2:])
//Output
['cherry', 'orange', 'kiwi']
- Getting a slice with a step:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[1::2])
//Output
[2, 4, 6, 8, 10]
- Using negative indexing in a slice:
fruits = ["apple", "banana", "cherry", "orange", 'kiwi']
print(fruits[-3:-1])
//Output
['cherry', 'orange']
These are just a few basic examples of using list slicing in Python. You can use slicing to extract any portion of a list based on the start and end indices and the step size, allowing you to work with subsets of data easily and efficiently.
List methods
Python lists have some built-in methods that you can use to perform several operations.
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value. |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
0 Comments
Please do not add Any Spam link in Comments !