Category
📚
LearningTranscript
00:00List functions and methods.
00:11Concepts discussed in this topic are
00:13Introduction to operations on lists
00:16Appending elements to a list
00:19Updating elements to a list
00:21Deleting elements from a list
00:23List functions and methods
00:26Recapitulation of the topic
00:30Python provides methods that operate on lists.
00:33Many of these methods modify the list.
00:36We can perform various operations
00:38such as appending, updating, deleting, etc.
00:44Appending elements to a list means
00:46adding elements to an existing sequence.
00:49The append, extend and insert methods
00:53add items to the list.
00:56The append method adds a single item
00:59to the end of the list.
01:01The extend method adds items from another list
01:04or any sequence to the end.
01:07The insert method inserts an item
01:09at a specific position
01:11as indicated by the index number
01:13and moves the remaining items to the right.
01:18Updating an element of the list
01:19is accomplished by assessing the elements
01:22and modifying its value in place.
01:25We can update a single element
01:27or part of the list.
01:31There are several ways to remove elements
01:33from a list.
01:35If index is known,
01:36we can use pop method
01:38or del statement.
01:40If the element or elements
01:41to be deleted is known,
01:43then remove method can be used.
01:45To remove multiple elements,
01:47we can use del statement
01:49with the list slicing.
01:51Using assignment operator
01:53The list manipulation methods
01:57such as index, append, pop, extend,
02:01insert, remove, delete
02:03can be applied to list
02:05as per following syntax.
02:07List object dot method name
02:09The dot operator is used
02:12to access built-in methods
02:13of list objects.
02:15The index method
02:18returns the index
02:19of first matched items
02:20from the list.
02:22In the given example,
02:24the method returns
02:25the index of first value,
02:2715,
02:28even if there is another 15
02:29in the list.
02:31If the element is not in the list,
02:33an error will be raised.
02:37The append method
02:39adds an item
02:39to the end of the list
02:41and returns no value.
02:43For example,
02:44to add one more fruit
02:46in a list containing fruits,
02:48use append method.
02:50We see from this example
02:51that append method
02:52does not return the new list,
02:55just modifies the original one.
02:58The extend method
03:00is used for adding
03:01multiple items to a list.
03:03This method
03:04takes a list
03:05as an argument
03:06and appends
03:07all of the elements
03:08of the argument list
03:09to the list object
03:10on which this method
03:11is applied.
03:12The insert method
03:15is used
03:16to insert an item
03:17at a given position.
03:19The syntax
03:19is given here.
03:21The method
03:22takes two arguments,
03:23index of the element
03:24and the element
03:25to be inserted.
03:27It returns no value.
03:30L1 contains odd numbers
03:32between 1 to 10,
03:34but digit 7
03:35is missing in the list.
03:36Insert digit 7
03:38before digit 9
03:39in the list.
03:41Now,
03:42here is the time
03:42to test your knowledge.
03:46There are two ways
03:47of using the pop method.
03:49The first,
03:50with no parameter,
03:51will remove
03:52and return
03:52the last item
03:53of the list.
03:54The second,
03:55if you provide
03:56a parameter
03:57for the position,
03:58pop will remove
03:59and return
04:00the item
04:00at that position.
04:03The pop method
04:04is used
04:05to remove
04:05an item
04:06from the list.
04:07The method
04:07removes an element
04:08from the given
04:09position in the list
04:10and returns
04:11the removed value.
04:14The remove method
04:15removes the first occurrence
04:16of given item
04:17from the list.
04:19The syntax
04:19is given here.
04:25The remove method
04:27raises an error
04:27if we try to remove
04:29an element
04:29which is not present
04:31in the list.
04:34We have pop method
04:36to remove any element
04:37from the list.
04:38Then why remove method
04:39is used?
04:41Pop method
04:41removes an element
04:42whose position
04:43is given.
04:45If the index
04:45or position
04:46is not known,
04:47we only know
04:48the value,
04:49then remove method
04:50is used.
04:52The clear method
04:53removes all the items
04:55from the list
04:56and the list
04:56becomes empty.
04:59Del statement
05:00deletes all the elements
05:02and the list object too.
05:03Unlike del statement,
05:05clear method
05:06removes only
05:07the elements
05:08and not the list element.
05:10After clear,
05:11the list object
05:12still exists
05:13as an empty list.
05:16The count method
05:17returns the count
05:18of the item
05:19that is passed
05:20as an argument.
05:23What if the argument
05:25in the count method
05:26does not exist
05:27in the list?
05:28The method
05:29returns 0.
05:30The reverse method
05:36reverses the items
05:37of the list
05:38and does not
05:38return anything.
05:43Does the reverse method
05:45create a new list?
05:47No.
05:47It does not create
05:48a new list,
05:49just reverses the list.
05:53The sort method
05:54sorts the items
05:55of the list
05:56by default
05:57in ascending order.
05:58It does not
05:59create a new list.
06:01It does not
06:02return anything.
06:04Write a code
06:05to sort the list
06:06in decreasing order.
06:08To sort the list
06:09in decreasing order,
06:11set reverse
06:11is equal to true.
06:15The following table
06:17provides a summary
06:18of the list methods.
06:20Append method
06:21adds a new item
06:22to the end
06:23of a list.
06:24Insert method
06:25inserts a new item
06:27at the position given.
06:29Pop method
06:30without parameters
06:31removes and returns
06:32the last item.
06:34Pop method
06:35with position
06:36removes and returns
06:37the item at position.
06:39Sort method
06:40modifies a list
06:42to be sorted.
06:43Reverse method
06:44modifies a list
06:45to be in reverse order.
06:48Index method
06:49returns the position
06:50of first occurrence
06:51of item.
06:53Count method
06:53returns the number
06:55of occurrences
06:55of item.
06:57Remove method
06:58removes the first
07:00occurrence of item.
07:02Find the errors
07:04in the given code.
07:05The method
07:06is trying to remove
07:07the element
07:08which does not exist
07:09in the list,
07:10so the error
07:11is raised.
07:13Test your knowledge
07:14of Python functions.
07:16Match the appropriate
07:17list method
07:18to perform
07:18the following tasks.
07:20Lesson
07:21points
07:213
07:21iyim
07:226
07:221
07:222
07:232
07:242
07:241
07:242
07:252
07:253
07:262
07:263
07:263
07:274
07:27...
07:273
07:284
07:294
07:305
07:305
07:315
07:326
07:325
07:324
07:336
07:345
07:356
07:357
07:3610
07:367
07:377
07:388
07:388
07:389
07:399
07:409
07:409
07:4210
07:438
07:4310
07:4420
07:447
07:459
07:4510
07:4512
07:4611
07:4710
07:4811
07:4810
07:4811
07:49You