"Warning: Learning Python Lists May Cause Extreme Confidence!"


What is Data Structure?
Data structures are a way of organizing and storing data so that they can be accessed and worked with efficiently. They define the relationship between the data, and the operations that can be performed on the data.
Built-in data structures in Python can be divided into two broad categories: mutable and immutable. Mutable (from Latin mutabilis, "changeable") data structures are those which we can modify -- for example, by adding, removing, or changing their elements. Python has three mutable data structures: lists, dictionaries, and sets. Immutable data structures, on the other hand, are those that we cannot modify after their creation. The only basic built-in immutable data structure in Python is a tuple.
Let's start with the mutable data structures: lists, dictionaries, and sets.
Lists
:
Lists in Python are implemented as dynamic mutable arrays which hold an ordered collection of items. They are heterogenous in nature. For instance, you can store integers, strings, and even functions within the same list.
Lists are useful when we want to store a collection of different data types and subsequently add, remove, or perform operations on each element of the list
We can create a list in python as shown below.
Example: Creating List
List1 = [] #empty list
List2 = [1,2,3] #list of integers
List3 = [1, 2, 3, "Hi", 2.3] #list with multiple data types
print(List1)
print(List2)
print(List3)
Output
[]
[1,2,3]
[1, 2, 3, 'Hi', 2.3]
Nested List:
A list within another list is referred to as a nested list in Python. We can also say that a list that has other lists as its elements is a nested list.
Example: Creating Nested List
List4 = ['One',[1,2,3],[4,5,6]]
print(List4[1][1])
print(List4[2][2])
Output:
2
6
Indexing:
List elements can be accessed by the assigned index. In python starting index of the list, sequence is 0 and the ending index is (if N elements are there) N-1.
There are 2 types of indexing:
- Positive indexing
Here we defined a list of colors. Each item in the list has a value(color name) and an index(its position in the list). Python uses zero-based indexing. That means, the first element(value ‘red’) has an index 0, the second(value ‘green’) has index 1, and so on.
Negative Indexing
In negative indexing system -1 corresponds to the last element of the list(value ‘black’), -2 to the penultimate (value ‘white’), and so on.
Slicing:
Slicing is the extraction of a part of a string, list, or tuple. It enables users to access the specific range of elements by mentioning their indices.
Syntax: Object [start:stop:step]
“Start” specifies the starting index of a slice
“Stop” specifies the ending element of a slice
You can use one of these if you want to skip certain items
Lets try an Example:
numbers = [10, 20, 30, 40, 50, 60] print(numbers[1:4]) print(numbers[:3]) print(numbers[::2]) Output [20, 30, 40] [10, 20, 30] [10, 30, 50]
You can also apply this method of slicing to strings:
text = "Python Slicing" print(text[7:]) print(text[::-1]) Output Slicing gnicils nohtyP
You can even extract alternate elements from a list with slicing:
data = [100, 200, 300, 400, 500] alternate = data[::2] print(alternate) Output [100,300,500]
List methods:
List methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, we’ll explore all Python list methods with a simple example.
append(): Adds an element to the end of the list.
copy(): Returns a shallow copy of the list.
clear(): Removes all elements from the list.
count(): Returns the number of times a specified element appears in the list.
extend(): Adds elements from another list to the end of the current list.
index(): Returns the index of the first occurrence of a specified element.
insert(): Inserts an element at a specified position.
pop(): Removes and returns the element at the specified position (or the last element if no index is specified).
remove(): Removes the first occurrence of a specified element.
reverse: Reverses the order of the elements in the list.
sort(): Sorts the list in ascending order (by default).
Examples of List Methods
append():
Syntax: list_name.append(element)
In the code below, we will add an element to the list.
a = [1, 2, 3]
a.append(4)
print(a)
Output
[1, 2, 3, 4]
copy():
Syntax: list_name.copy()
In the code below, we will create a copy of a list.
a = [1, 2, 3]
b = a.copy()
print(b)
Output:
[1,2,3]
clear():
Syntax: list_name.clear()
In the code below, we will clear all elements from the list.
a = [1, 2, 3]
a.clear()
print(a)
Output
[]
count():
Syntax: list_name.count(element)
In the code below, we will count the occurrences of a specific element in the list.
a = [1, 2, 3, 2]
print(a.count(2))
Output
2
extend():
Syntax: list_name.extend(iterable)
In the code below, we will extend the list by adding elements from another list.
a = [1, 2]
a.extend([3, 4])
print(a)
Output
[1, 2, 3, 4]
index():
Syntax: list_name.index(element)
In the code below, we will find the index of a specific element in the list.
a = [1, 2, 3]
print(a.index(2))
Output
1
insert():
Syntax: list_name.insert(index, element)
In the code below, we will insert an element at a specific position in the list.
a = [1, 3]
a.insert(1, 2)
print(a)
Output
[1, 2, 3]
pop():
Syntax: list_name.pop(index)
In the code below, we will remove the last element from the list.
a = [1, 2, 3]
a.pop()
print(a)
Output
[1, 2]
remove():
Syntax: list_name.remove(element)
In the code below, we will remove the first occurrence of a specified element from the list.
a = [1, 2, 3]
a.remove(2)
print(a)
Output
[1, 3]
reverse():
Syntax: list_name.reverse()
In the code below, we will reverse the order of the elements in the list.
a = [1, 2, 3]
a.reverse()
print(a)
Output
[3, 2, 1]
sort():
Syntax: list_name.sort(key=None, reverse=False)
In the code below, we will sort the elements of the list in ascending order
a = [3, 1, 2]
a.sort()
print(a)
Output
[1, 2, 3]
Conclusion
Python lists are one of the most important tools for beginners. You can use them to store many items in one place, like numbers, words, or even other lists.
With lists, you can easily add, remove, change, and check items. You also learned how to loop through them, slice them, and use some common functions like append()
, pop()
, sort()
and so on.
Learning lists is a big step toward becoming better at Python. Once you're good with lists, learning other data structures will be much easier.
#chaicode
Subscribe to my newsletter
Read articles from SHEHANAJ SHAIK directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
