List Part 2

NPTEL-NOC IITM
2 Sept 201913:49

Summary

TLDRThis lecture delves into list manipulation in Python, teaching how to modify, add, and remove elements. It covers direct index assignment for modification, using the 'append' method to add elements or lists, and the 'insert' function for position-specific additions. The session also explains removal techniques, including 'del' for specified index removal, 'remove' for first-match deletion, and 'pop' for displaying and removing elements at given indices, all illustrated with clear examples.

Takeaways

  • 📝 Modifying Lists: The lecture covers how to alter elements in a list by assigning new values using index positions or built-in functions.
  • 🔍 Indexing: Lists can be modified at both the top level and sublevel components using specific index values.
  • 🔑 Top Level Change: Demonstrated how to change the value of the 'number of employees' in a list by directly accessing its index.
  • 🔑 Sublevel Change: Showed the process of altering a specific employee's name within a nested list structure.
  • 📚 Built-in Functions: Python provides several built-in functions like 'append', 'insert', 'del', 'remove', and 'pop' for list manipulation.
  • 📌 Append Method: Explained how to use 'append' to add elements to the end of a list or concatenate a list with another.
  • 📍 Insert Function: Described the 'insert' function to add elements at a specific position within a list.
  • 🗑️ Deletion Techniques: Covered three methods to remove elements from a list: 'del' for removing by index, 'remove' for the first occurrence, and 'pop' to remove and return an element at a specific index.
  • 🔄 List Concatenation: Illustrated adding a new list to an existing list using the 'append' method to create a multi-level list.
  • 📈 Practical Examples: Provided practical examples of modifying a list with employee details, including IDs, names, and ages.
  • 👋 Conclusion: Summarized the main points on list manipulation, emphasizing the use of index numbers and built-in functions for adding and removing elements.

Q & A

  • How can you modify the elements of a list in Python?

    -You can modify the elements of a list in Python by directly assigning a new value to a specific index or by using built-in functions like 'append', 'insert', 'remove', and 'pop'.

  • What is the purpose of assigning a new value to an index in a list?

    -Assigning a new value to an index in a list allows you to change or update the value of an element at a specific position within the list.

  • Can you explain the concept of top-level and sub-level components in a list?

    -In a list, top-level components refer to the main elements of the list, while sub-level components refer to elements within those top-level elements, essentially creating a nested list structure.

  • How do you change the number of employees in a list using indexing?

    -To change the number of employees, you would assign a new value to the index that corresponds to the 'number of employees' in the list, for example, `employee_list[2] = 5`.

  • What does the 'append' method do in Python lists?

    -The 'append' method adds an object to the end of the list. If the index is not specified, the object is added at a new level in the existing list.

  • Can you add a list to another list using the 'append' method?

    -Yes, you can add a list to another list using the 'append' method, which is also known as concatenation of lists.

  • How does the 'insert' function differ from 'append'?

    -The 'insert' function adds an object at a specified position in the list, whereas 'append' always adds the object to the end of the list.

  • What are the different methods to remove elements from a list in Python?

    -The methods to remove elements from a list in Python are 'del', 'remove', and 'pop'. 'del' removes an element at a specified index, 'remove' removes the first matching element, and 'pop' removes and returns the element at a specified index.

  • How does the 'remove' function work in Python lists?

    -The 'remove' function searches for the first occurrence of a specified object in the list and removes it.

  • What does the 'pop' function return when removing an element from a list?

    -The 'pop' function not only removes the element at the specified index but also returns that element.

  • Can you provide an example of how to use the 'del' statement to remove a sublist from a list?

    -To remove a sublist, you would use the 'del' statement followed by the list name and the index of the sublist, for example, `del employee_list[3]` to remove the 'age' sublist from the 'employee_list'.

Outlines

00:00

📝 Modifying List Elements in Python

This paragraph introduces the concept of modifying list components in Python, including changing elements based on index positions and utilizing built-in functions for such modifications. It explains how to update both top-level and sub-level components of a list. The example given involves an 'employee_list' with details such as employee IDs, names, and the total number of employees. The paragraph demonstrates how to change the number of employees from 4 to 5 and how to replace an employee's name from 'John' to 'Karan' using direct index assignment.

05:03

🚀 Adding Elements to Lists with Append and Insert

The second paragraph delves into the use of the 'append' method to add elements to the end of a list or to concatenate lists. It also introduces the 'insert' method for adding elements at a specified position within a list. The syntax for both methods is explained, along with examples of adding both individual elements and entire lists to an existing 'employee_list'. The paragraph illustrates the process of updating the list with new employee IDs, names, and a new list of employee ages, as well as how to insert a new employee ID at the beginning of the list.

10:06

❌ Removing Elements from Lists Using del, remove, and pop

The final paragraph covers the various methods to remove elements from a list in Python, namely 'del', 'remove', and 'pop'. It explains the syntax and functionality of each method, providing examples for their application. The 'del' method is shown removing an entire sub-list, such as the ages from the 'employee_list'. The 'remove' method is demonstrated to remove the first occurrence of a specific element, like removing 'Ram' from the employee names. Lastly, the 'pop' method is explained to remove an element at a specified index and return the removed value, exemplified by removing the fourth ID from the employee list.

Mindmap

Keywords

💡List

A list in Python is a built-in data structure that can hold an ordered collection of items, which may be of different types. In the context of the video, lists are used to store and manage data such as employee IDs, names, and other attributes. The script discusses modifying lists by adding and removing elements, which is central to the video's theme of list manipulation.

💡Indexing

Indexing refers to the method of accessing elements in a list by their position. In Python, indexing starts at 0, and it allows for the retrieval or modification of specific elements. The script uses indexing to demonstrate how to change values within a list, such as updating the number of employees or modifying an employee's name.

💡Modify

To modify in the context of lists means to change or update the elements within the list. The script covers various methods of modification, including direct assignment using index positions and the use of built-in functions to add or remove elements, which is essential for understanding how to work with lists in Python.

💡Append

The append method in Python is used to add an item to the end of a list. The script explains how append can be used to add new employees to the list by specifying the new ID and name, demonstrating the dynamic nature of list manipulation in Python.

💡Concatenation

Concatenation in the context of lists refers to the process of joining two lists together to form a new list. The script mentions this concept when discussing the use of append to add a list of ages to the existing employee list, showcasing how lists can be expanded with additional data.

💡Insert

The insert method is used to add an element to a list at a specified position, rather than at the end. The script illustrates how to use insert to add an employee ID at the beginning of the list, which is a key concept in understanding how to position elements within a list.

💡Remove

The remove function in Python is used to delete the first occurrence of a value in a list. The script demonstrates the use of remove to delete an employee's name from the list, emphasizing the ability to selectively delete elements based on their value.

💡Pop

The pop method is used to remove an element from a list at a specific index and returns the value of the removed element. The script explains how pop can be used to remove an employee ID from the list, showing how elements can be both removed and retrieved from a specific position.

💡Del

The del statement in Python is used to delete an object. In the context of lists, del can be used to remove an element or a sublist by specifying the index. The script uses del to demonstrate the removal of the entire 'age' sublist from the employee list, illustrating how to delete complex components of a list.

💡Sublevel Component

A sublevel component refers to an element within a nested list structure. The script mentions sublevel components when discussing how to modify elements within the nested lists that represent employee details, highlighting the complexity of list structures and their manipulation.

💡Top Level Component

A top-level component in a list refers to the main elements of the list before any nesting occurs. The script uses the term to differentiate between modifying elements at the main list level, such as changing the number of employees, versus modifying elements within nested lists, like changing an employee's name.

Highlights

Introduction to modifying lists in Python, including adding and removing elements.

Modifying list elements using index positions and inbuilt functions.

Changing top level components of a list by assigning new values to specific indices.

Modifying sublevel components by accessing nested lists within the main list.

Using the append method to add elements at the end of a list.

Appending a new list to an existing list for concatenation.

Syntax and usage of the append method for adding elements to a list.

Using the insert method to add elements at a specified position in a list.

Syntax and parameters of the insert method for adding elements at a given position.

Removing elements from a list using the del statement.

Using the remove method to delete the first occurrence of a specific element.

Syntax and usage of the remove method for removing elements by value.

Using the pop method to remove and return an element at a specified index.

Syntax and parameters of the pop method for removing elements by index.

Summary of list manipulation techniques including append, insert, del, remove, and pop.

Transcripts

play00:08

Welcome to the lecture.

play00:16

In the previous lecture, we saw how to create a list and also how to access the elements

play00:21

in the list and also we saw indexing in Python.

play00:24

So, in this lecture, we will see how to modify the lists; so, basically how to add the elements

play00:32

and also how to remove the elements from the list.

play00:35

So, first one is modifying components of a list.

play00:39

So, elements inside the list can be modified using the two methods; the first method is

play00:44

we can assign the new element value based on the index position.

play00:48

So, second one is using the inbuilt functions.

play00:52

So, we can give as an input to the function along with the index value.

play00:57

Modifying components of a list using the index: so, first we will need to assign the values

play01:04

to be changed to the corresponding index of a list.

play01:08

So, there are two levels in a list.

play01:11

So, one is called as a top level component of a list and other one is called as a sublevel

play01:15

component of a list.

play01:16

So, let us say if I wanted to change the value in top level component of a list.

play01:21

So, this is our existing list so, which we have already created employee underscore list

play01:26

which has a levels id which is 1, 2, 3 and 4 employee names Ram, Preethi, Sathish and

play01:35

John and the number of employees which as 4.

play01:37

Let us say if I wanted to change the value of 4 to 5 so, which is the number of employees.

play01:44

So, we have total number of employees is 4 if I wanted to change to 5.

play01:49

So, then ids stands for the level 0 Ram, Preethi, Sathish, John so, which is employee names

play01:56

which is at the level of 1 and number of employees at the level of 2.

play02:01

Employee underscore list 2.

play02:02

So, I am assigning it to 5.

play02:04

So, if I print the updated list so, four will be replaced with 5.

play02:10

So, the encircle ones has been replaced.

play02:14

Let us say if I wanted to change the value in sub level component of a list.

play02:19

So, this is our list.

play02:21

So, if I wanted to change John to Karan, how will you do that?

play02:26

So, 1 2 3 4 which is basically id; again its a level 0, employee names is level 1 and number

play02:33

of employees is level 2.

play02:35

So, if I give employee underscore list 1 which is our top level component so, our sub level

play02:43

is 3.

play02:44

So, John is placed in the fourth position which has a index number 3 so, I wanted to

play02:49

replace with Karan.

play02:50

So, I am giving inside the double quotes.

play02:53

So, if I print the updated list so, John will be replaced with Karan.

play03:01

Next we look at the inbuilt functions available in python.

play03:04

So, let us say if I wanted to modify components using the append method.

play03:09

So, what is append does is it is an adds an object at the end of the list.

play03:15

So, we let us look at the syntax of the append list_name within the square brackets, you

play03:20

have to specify the index number dot append and inside the parenthesis you have to specify

play03:27

the object.

play03:28

So, if the index is not specified the elements, the object gets added at the new level in

play03:36

the existing list.

play03:37

There are two ways to add an object to a list.

play03:41

First is you can add an element to a list, second one you can add a list to a list so,

play03:47

which is called as the concatenation of a list.

play03:49

First we will see how to add an element to a list using the append method.

play03:55

Let us say if I wanted to add an element to a list.

play03:58

So, today one person has joined.

play04:01

So, employee id will be increased by 1 and also employee_name will be increased by 1

play04:07

right.

play04:08

Let us say if I wanted to add the id 5 in the employee list.

play04:13

So, employee underscore list is a list name inside the square brackets, we have to mention

play04:20

the index number which is zero dot append of 5.

play04:25

Similarly I wanted to add Nirmal to the level employee_name in the list.

play04:29

Employee underscore list again, it will be instead of 0, it will be 1 dot append Nirmal.

play04:39

If we print the updated lists; now it will have 5 ids 1, 2, 3 and 4 and 5 and we have

play04:47

5 employee names, Ram, Preethi, Sathish, Karan and Nirmal and also the number of employees

play04:53

is 5.

play04:55

Let us say if I wanted to add a list to a list.

play04:57

Adding a new list age to the existing employee underscore list.

play05:02

So, now what I will do is I will create a age as a list and I will appended to the existing

play05:09

employee underscore list.

play05:11

So, we already have 5 employees.

play05:13

So, the respective ages will also be created as a list.

play05:17

So, I am creating a age as a list which is equal to 23, 25, 36, 43 and 52.

play05:26

So, if I wanted to append to the existing list so, I will call the employee underscore

play05:31

lists dot append on the sets of values.

play05:34

So, this is a one way or else you can do employee underscore list dot append age as well.

play05:42

So, this list its gets added as a new level at the end of the existing list.

play05:49

So, if you print the updated list so, we already had id employee name number of employees.

play05:57

Now since we have added age to the existing employee underscore list so, it will be added

play06:04

at the last.

play06:05

So, next we will use a inbuilt function called insert.

play06:09

So, in append, we saw how to add a list at the last.

play06:13

So, if you wanted to add an element at the specified position, then insert command works

play06:18

well.

play06:19

So, what does insert does is it adds an object at the given position in the list.

play06:24

So, let us look at the syntax; list_name index dot insert inside the parenthesis, you have

play06:33

to specify the position and as well as a object.

play06:39

So, this is our existing list.

play06:41

So, 1, 2, 3 ids and the 5 employee names number of employees and the respective ages.

play06:48

Let us say if I wanted to add id number 6 to the id at the first position.

play06:56

So, it will be employee underscore list list name and we have to specify the index.

play07:01

So, the id as a level zero dot insert position is at which position you wanted to insert.

play07:09

So, I am inserting at the first position.

play07:13

So, corresponding index will be 0 in python indexing starts from 0 to n minus 1.

play07:18

So, I wanted to add the value of 5.

play07:21

So, in the object I will specify 6.

play07:25

So, if you print the updated list 6 id will be placed at the first position.

play07:31

So, the indexing will be this 0 remaining will be same.

play07:38

Next we will see how to remove elements from the list.

play07:41

So, there are various methods we can use del, remove and pop.

play07:47

So, let us look at one by one.

play07:49

So, del it removes the object at the specified index number, let us look at the syntax.

play07:55

So, if you give del is a key word list name and inside the square brackets you have to

play08:01

mention the index 1 and the index 2.

play08:05

Index 1, it corresponds to the index number of the top level components of the list which

play08:12

has to be dropped.

play08:13

Similarly, index two is a index number of the sub level components to be dropped.

play08:20

So, this is our existing list.

play08:22

So, if I wanted to drop the last level that is age id, it is a level zero and Ram, Preethi,

play08:30

Sathish is the employee names.

play08:31

So, if the level is 1 and number of employees which is 5 is level 2 and age is at the level

play08:37

3.

play08:38

So, if I wanted to remove the age from the employee list so, it will be del of employee

play08:44

underscore list and 3 which is basically the index number.

play08:49

So, if you print the updated list, now our age list will be removed from the employee

play08:57

underscore list next look at how to use the remove option.

play09:02

So, remove what it does is it removes a first matching object from a list.

play09:08

So, we will look at the syntax list_name inside the square brackets, we have to mention the

play09:15

index number dot remove and inside the parenthesis, we have to specify the object.

play09:20

So, this is our existing lists.

play09:22

So, after we deleted the age.

play09:25

So, let us say if I wanted to remove ram from the employee name in the employee list.

play09:32

So, employee underscore list so, the index number is one dot remove inside the double

play09:39

quotes Ram.

play09:40

So, what it does is it searches for the first occurrence of the ram and then it will be

play09:46

removed from the list.

play09:47

So, we have only one Ram.

play09:49

So, we have one Preethi, one Sathish, one Karan and one Nirmal.

play09:53

So, if you give dot remove Ram so, it will remove the Ram from the employee underscore

play09:59

list.

play10:00

So, when you print the updated list, Ram will be removed from the employee underscore list.

play10:05

So now you will have 6 ids 6, 1, 2, 3, 4 and 5 and you will have 4 employees which is Preethi,

play10:13

Sathish, Karan, Nirmal and you will have the number of employees is equal to 5 since has

play10:19

ram has occurred once it has been removed.

play10:22

So, let us consider another list create a list which has salary High, Low, Medium and

play10:31

Low.

play10:32

So, if I wanted to remove the first occurrence of low which is so, High stands for the index

play10:37

number 0, Low stands for the index number 1, Medium stands for a index number 2; again

play10:43

Low stands for the index number 3.

play10:45

So, if I wanted to remove the first occurrence of low which since in the index position of

play10:50

1, then if I give salary dot remove low so, it will be removed from the salary list.

play10:59

So, if you print the updated list.

play11:01

So, it returns the value of high, medium and low.

play11:06

So, the corresponding index position now it will be changed to 0, 1 and 2.

play11:10

Next we will the modify the components using the pop function.

play11:16

So, pop what it does is it displays the objects that is being removed from the list at the

play11:22

specified index number.

play11:24

So, let us look at the syntax list_name index1 dot pop and index2.

play11:31

So, the index 1 corresponds to the index numbers of the top level of components to be dropped,

play11:38

index 2 it corresponds to the sub level of components to be dropped.

play11:42

So, this is our existing list.

play11:44

So, I have ids and 4 employees and the number of employees is 5.

play11:51

Let us say if I wanted to remove the fourth id from the fifth position of the employee

play11:58

list so, in the id.

play12:00

So, if I give employee underscore list 0 dot pop 4.

play12:05

So, it pops ups the element in the list is going to be removed so, which is four is going

play12:13

to be removed from the id level.

play12:15

If we print the updated list 6, 1, 2, 3, 4 will be removed from the level id and you

play12:24

will have 4 employee names Preethi, Sathish, Karan and Nirmal and you will have number

play12:29

of employees is equal to 5.

play12:32

So, let us summarize.

play12:33

So, first we saw how to manipulate the list using the index number and we also saw some

play12:40

of them built in functions.

play12:42

So, first we saw append.

play12:44

So, append it adds an element at the end of the list and also it creates a new list; it

play12:53

is also called as a concatenation of a list.

play12:56

So, next we saw insert.

play12:58

So, if I wanted to add an element at the specified position insert works very well.

play13:05

Next we saw how to remove the elements.

play13:07

So, there are three ways one is del; first one is del so, it removes a element at the

play13:12

specified position.

play13:14

So, if we give remove, it removes the first matching element from the list.

play13:20

If I give pop it displays and removes element at the specified position of the lists.

play13:27

Thank you.

Rate This

5.0 / 5 (0 votes)

Related Tags
Python ListsList ManipulationProgramming TutorialData StructuresCode ExamplesIndexing BasicsAppend MethodInsert FunctionRemove ElementsPop Operation