There are several Data types used in Python like
- Strings
- Tuple
- Set
- Dictionary
- List
In this post, we are going to have a clear idea about List and its predefined modules.
The list is a mutable data type which means we can change memory Allocation based on our requirements and it is represented with square brackets [].list can accept any kind of data ex: int, float, string.
a = [1,1.2,'python',True] # boolean = True , False
for i in a:
print(type(i))
using the index position we can access the values which are inside the list. for example, for accessing the value 1.2 its index is 1 so we can call it using logic a[1].
mylist = [4,9,6,2]
print(mylist[0])
print(mylist[1])
print(mylist[2])
print(mylist[3])
For accessing Multiple values we can use a concept called slicing operations. where the forward slicing index starts from 0 and in reverse slicing index starts from -1.
mylist = [1, 2, 3, 4, 5, 6]
# from 2nd element till 5th element
print(mylist[1:5])
# from start till 5th element
print(mylist[:5])
# from 5th element till end
print(mylist[4:])
# from start till 3rd last element
print(mylist[:-2])
# from 4th last element till 3rd last element
print(mylist[-4:-2])
# from 3rd element till 3rd last element
print(mylist[2:-2])
# all elements
print(mylist[:])
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# from 2nd element till 9th element
print(mylist[1:9])
# from 2nd element till 9th element in step 2
print(mylist[1:9:2])
# from 2nd element till 9th element in step 3
print(mylist[1:9:3])
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# from 3rd element till 9th element
print(mylist[2:9])
# from 3rd element till 9th element in step 2
print(mylist[2:9:2])
# from 3rd element till 9th element in step 3
print(mylist[2:9:3])
# from start till 9th element in step 2
print(mylist[:9:2])
# from 3rd element till end in step 3
print(mylist[2::3])
# elements from start till end in step 2
print(mylist[::2])
There are some predefined modules that help us to manipulate the values inside a list in Multiple ways.
- append
- clear
- extend
- pop
- insert
- del
- remove
- Membership function
- sort
- Math operations
- append is used to add the values at the end of the list.
#Adding Elements to Python List
#Adding a Single Element to Python List
mylist = [1, 2, 3, 4]
mylist.append(5) # 5 is added to the list
print(mylist) # printing changed list
- clear is used to remove clear the entire data from the list so it will be empty.
#Python clear()
colors = ["Blue", "Green", "Red", "Orange", "Yellow", "Brown"]
colors.clear() # removing all element from list
print(colors) # printing updated list
- extend is used to add another list
a = [1, 2 , 3, 4]
a.extend([10,100])
print(a)
- pop is used to remove the last value from the list but if u specify any values it takes the value as an index and removes the index-located value.
a = [1, 2 , 3, 4]
a.pop()
#a.pop()
print(a) # output = [1,2,3]
if we specify any value
a = [1,2,3,4]
a.pop(2)
output = [1,2,4] because 2nd index value is removed from the given list
- Insert is used to add value where ever we want using the index concept
mylist = [1, 2, 3, 4]
mylist.insert(2, 5) # adding 5 at index 2 of the list
print(mylist) # printing changed list
- del keyword will delete the value from the list
colors = ["Blue", "Green", "Red", "Orange", "Yellow", "Brown"]
del colors[2:4] # deleting from 3rd element to fourth element
print(colors) # from 2nd till 3rd index values will be deleted using (n-1) concept
- Instead of using index concepts every time we can use the remove function which directly takes the value and removes it from the given list.
#Python remove()
colors = ["Blue", "Green", "Red", "Orange", "Yellow", "Brown"]
colors.remove('Green') # removing "Green" from list
print(colors)
# Green is removed from the given list
- The membership function is useful for finding the specified value present in a given list or not. If it’s present means it returns true and if not it returns false.
#Python List Membership Test
mylist = [1, 2, "Blue", "Green"]
print(2 in mylist) # True
print(4 in mylist) # False
print("Green" not in mylist) # True
- sort function helps us to sort values from ascending order to descending order or from descending order to ascending order.
a = [4,3,1,2] # ascending order
a.sort()
print(a) # output = [1, 2, 3, 4]
a = [4,3,1,2] # descending order
a.sort(reverse = True) # reverse = True makes sort in descending order
print(a) # output = [4, 3, 2, 1]
- Math Operations
mylist = [1, 2, 3, 4, 5, 6]
print(len(mylist)) # len keyword is used to find the number of values
# Output = 6
mylist = [3, 2, 1, 5, 6, 4] # max keyword used to find maximum value
print(max(mylist))
# output = 6
mylist = [3, 2, 1, 5, 6, 4] # min keyword used to find minimum value
print(min(mylist))
# output = 1
For more code examples You can collect them from my GitHub page
https://github.com/saikamal3344/Medium-Blogs/blob/main/Python%20List.ipynb
a = [1,1.2,'python',True] # boolean = True , False
for i in a:
print(type(i))