Lists in Python - Basic Operations and Its Methods - Python Basics for Beginners #19

Programming

Posted on December 11, 2024
by Rajjit Laishram Admin

Updated on December 11, 2024

Cover image for Lists in Python - Basic Operations and Its Methods - Python Basics for Beginners #19
LISTS

A data type in sequence, in which elements are written as a list of comma separated values between square brackets. Example: list_var = [val1, val2, val3, ...]

It is a mutable data type, where the elements can be changed.

Its values are accessed using index. It can be sliced and concatenated like the strings.

An example is given below:

list_num =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
print("The list is: ", list_num)
# accessing the values
print("The first value is: ", list_num[0])
print("The fifth value is: ", list_num[5])
LIST SLICING

It allows us to access a portion of a list by a range of indices.

Uses the parameters: start, stop and step.

start: Beginning index of the slice. Default of start is 0.

stop: Ending index of the slice.

step: Interval between the elements in the list. Default of step is 1.

An example is given below:

# slicing
print("The first five values are: ", list_num[0:5])
print("list between index 2:5", list_num[2:5])
# print("list of [::2]", list_num[::2])
# print("list of [::3]", list_num[::3])
print("list of [1:3]", list_num[1:3])'''
UPDATING THE VALUES

We can update the values/elements of a list using direct assignment and also the operations below

# Updating the values
some_list = ["Happy", "Tomba", "Chaoba", "Thoiba"]
print("the names are:", some_list)
some_list[2] = "Tompok"
print("After updating...")
print("the names are:", some_list)
NESTED LIST

A list within another list is also possible.

We will look at an example.

# nested list
new_list = [1, 2, 3, 'a', ' ', [12, 13, 14, 15], [20, 21], "Tompok"]
print("The list is: ", new_list)

# beautify the print
i = 0
while i < len(new_list):
    print("List [",i,"]", new_list[i])
    i += 1
SOME BASIC OPERATIONS

# len: gives the length of the whole list

new_list = [1, 2, 3]
new_list2 = [11, 21, 31, 22, 768, 12,90, 100]
output_len = len(new_list)
print(output_len)

# in: checks if there is a specific item in the list and returns bool value

check_list = [12, 13, 14, 15] in new_list
print(check_list)

# not in: same as in but opposite of it

check_listNot = 55 not in new_list
print(check_listNot)

# concatenation: concatenate two lists

outputCon = new_list + new_list2
print(outputCon)

# max: finds the maximum item in the list

# min: finds the minimum item in the list

max_output = max(new_list2)
min_output = min(new_list2)
print("The max output is: ", max_output, " and the min output is: ", min_output)

# sum: performs the sum of elements in the list

list1 = [22.1, 45.2, 89.0]
list2 = [22, 35, 67, 12]
new_sum = sum(list1)
print(new_sum)

# all: returns True if all all elements in the list are True or if the list is empty. Otherwise, it returns False.

# any: returns True is at least one element in the list is True. Otherwise, it returns false.

num_list = [0] # true if empty
print(all(num_list))
num_list = [] # false if empty
print(any(num_list))

# list: creates a new list using the items and values given in the list

listItem = list("HelloTomba")
print(listItem)

# sorted: sorts the items/elements in the list

# sorted
num_list = [4, 9, 10, 44, 77, 90, 1, 54]
print(sorted(num_list))
LIST METHODS

# append(): appends or adds a new item in the list at the end of the list

# insert(): inserts a new item or element at a specific index

newList = ["Tomba", "Chaoba", "Thoiba"]
newList.append("Henry")
newList.insert(3, "Henry")
newList.insert(3, 12)
print(newList)

# count(): It is used to count the occurrences of a specified element in a list.

# index(): It is used to find the first occurrence of a specified element in a list and return its position.

newList = [1, 2, 3, 4, 56, 11, 7, 1, 67, 2, 3, 1, 3, 2, 2, 2, 9]
print(newList.count(2))
print(newList.index(31))

# pop(): It is used to remove and return an element from the list at a specified position. If no position is specified, it removes and returns the last element in the list.

# remove(): It is used to remove the first occurrence of a specified element from the list.

newList.pop(4)
print(newList)
newList.remove(6)
print(newList)

# reverse(): It is used to reverse the items in the list.

newList.reverse()
print(newList)

 # sort(): It is used to sort the items in the list.

newList.sort()
print(newList)

 # extend(): It is used to extend a list with the help of another list.

newList2 = [22, 45, 33]
newList.extend(newList2)
print(newList)
VIDEO EXPLANATION:

MAKE SURE TO WATCH THE FULL VIDEO AND BROWSE OTHER BLOGS here  Python Basics for Beginners #18

No comments found. Be the first one to comment!

Add a comment

NOTE: You cannot delete your comment once added, so please don't post unnecessary comments!