Join Regular Classroom : Visit ClassroomTech

Programming in Python – codewindow.in

Related Topics

Python Programing

fruits = ['apple', 'banana', 'orange']

In this example, we have created a list called fruits with three items: 'apple', 'banana', and 'orange'.

You can access individual items in a list by using their index, which starts at 0. For example, fruits[0] would give you 'apple'.

You can also modify items in a list by using their index. For example, fruits[1] = 'grape' would change the second item in the fruits list to 'grape'.

You can add new items to a list using the append() method. For example, fruits.append('pear') would add the string 'pear' to the end of the fruits list.

You can also remove items from a list using the remove() method. For example, fruits.remove('banana') would remove the string 'banana' from the fruits list.

In addition to these basic operations, Python provides a variety of built-in functions for working with lists, such as len() to get the length of a list and sorted() to sort the items in a list.

my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print(length)  # Output: 5

In the above example, len(my_list) returns 5, which is the number of elements in the my_list list.

The len() function can be useful in many situations, such as when you need to loop over a sequence a specific number of times, or when you need to check if a sequence is empty. It is also commonly used to check the length of a user input or a file that you are reading.

It’s important to note that the len() function only works on sequences, which means it cannot be used with other data types such as integers or floats. It is also not possible to use the len() function on objects that do not have a defined length, such as functions or methods.

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]

In the above example, my_list.append(4) adds the element 4 to the end of my_list.

The extend() method, on the other hand, adds multiple elements to the end of a list. To use extend(), you call the method on the list and pass the sequence of elements you want to add as an argument. Here’s an example:

my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

In the above example, my_list.extend([4, 5, 6]) adds the elements 4, 5, and 6 to the end of my_list.

The main difference between append() and extend() is that append() adds a single element to the end of a list, while extend() adds multiple elements to the end of a list. Additionally, extend() requires a sequence of elements as an argument, while append() requires a single element as an argument. It’s also worth noting that you can use the + operator to concatenate two lists, which is similar to using the extend() method. However, concatenation creates a new list, while extend() modifies the existing list.

my_list = [1, 2, 3, 4, 5]
my_list.insert(2, "new element")
print(my_list)  # Output: [1, 2, 'new element', 3, 4, 5]

In the above example, my_list.insert(2, "new element") inserts the string "new element" at index 2 in the list my_list. This shifts all the elements at and after the index by one position to the right.

It’s important to note that the insert() method modifies the original list and returns None. Also, if the index specified is greater than or equal to the length of the list, the element will be inserted at the end of the list.

The insert() method can be useful in situations where you want to add an element to a specific location in a list, rather than just appending it to the end. It can also be used to add multiple elements to a list at specific positions by calling insert() repeatedly.

Here’s an example of inserting multiple elements into a list at specific positions:

my_list = [1, 2, 3, 4, 5]
my_list.insert(2, "new element 1")
my_list.insert(4, "new element 2")
print(my_list)  # Output: [1, 2, 'new element 1', 3, 'new element 2', 4, 5]

In the above example, the string "new element 1" is inserted at index 2, and the string "new element 2" is inserted at index 4, shifting the elements to the right of each index to make room for the new elements.

my_list = [1, 2, 3, 2, 4]
my_list.remove(2)
print(my_list)  # Output: [1, 3, 2, 4]

In the above example, my_list.remove(2) removes the first occurrence of the element 2 from my_list.

The pop() method, on the other hand, removes the element at a specified index from the list and returns it. If no index is specified, pop() removes and returns the last element in the list. Here’s an example:

my_list = [1, 2, 3, 4, 5]
element = my_list.pop(2)
print(element)  # Output: 3
print(my_list)  # Output: [1, 2, 4, 5]

In the above example, my_list.pop(2) removes the element at index 2 (i.e., the third element) from my_list and returns it. The removed element is assigned to the variable element.

The main difference between remove() and pop() is that remove() removes a specified element from the list, while pop() removes an element at a specified index and returns it. Additionally, remove() modifies the list in place and returns None, while pop() modifies the list in place and returns the removed element.

It’s important to note that if you try to remove an element that doesn’t exist in the list using remove(), you’ll get a ValueError. Similarly, if you try to pop an index that doesn’t exist in the list using pop(), you’ll get an IndexError.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories