Skip to playerSkip to main contentSkip to footer
  • 2 days ago

Category

📚
Learning
Transcript
00:00Working with Lists
00:01Concepts discussed in this topic are
00:12Introduction
00:13Creating Lists
00:14Assessing Lists
00:16Traversing a List
00:17List Operations
00:18Recapitulation
00:20What is a list?
00:22A list is an object in Python that can hold multiple data items of different types.
00:28It can store a sequence of values belonging to any type.
00:34Before creating a list, we will study about the properties of a list.
00:38A list is an ordered set of elements where the elements can be of any type.
00:43A list is created by placing all the elements inside a square bracket, separated by commas.
00:50They are mutable, that is, they can be changed after they are created.
00:56Indexing List
00:57Each item or element corresponds to an index number, indicating the position in the list.
01:03Indices start with 0.
01:05For assessing an element of a list, indexing is used.
01:09Find the value of index 2.
01:12It will show third item in the list.
01:16How is a list created?
01:17To create a list, enclose the number of expressions in square brackets, separated by commas.
01:25Creating List Using Built-in Objects
01:27The list function creates a list from an iterable object.
01:32If no parameter is specified in the list, a new empty list is created.
01:37There are two ways to create an empty list.
01:40An empty list is created using just square brackets or by using list function.
01:45An empty list does not contain any elements.
01:50It is a list equivalent of zero or single quotes.
01:54If a list contains many elements, split it across several lines.
01:58The items in the list can also be assessed with a negative index number by counting backwards
02:04from the end of the list, starting at minus 1.
02:08Especially useful for a long list.
02:12A nested list contains list within a list.
02:15Let us see an example.
02:17A list can be created from the existing sequences.
02:22The syntax is given here.
02:24Consider the following examples.
02:26List L1 is created from a string Python.
02:30It generates individual elements from the individual letters of the string.
02:35List L2 is created from a tuple.
02:37T1 is a tuple.
02:40The eval is a built-in function of Python,
02:43which can be used to evaluate and return the result of an expression given as a string.
02:49The expression given as string values within the parenthesis of the eval method
02:54will be converted into integer type and returns the sum of two numbers.
02:59The result is also int type.
03:02Here is the output.
03:05A list can be created by using console.
03:09To enter data from keyboard, input method is most commonly used.
03:13This method is used to enter elements in the built-in type list object.
03:19All the data entered by the keyboard are string type.
03:22To enter integer type, use eval method.
03:27Assessing a list.
03:28For assessing an element of a list, indexing is used.
03:32The index is placed between the square brackets given after the name of the list.
03:38Each element has an index indicating the position in the list.
03:41Indices start at 0.
03:44The list L1 will show second item that is 34.
03:50Lists are sequences like strings.
03:52They also index their individual elements just like strings do.
03:57Consider the following examples.
03:59The elements are indexed in two ways, forward and backward.
04:04Forward indexing is done as 1, 2, 3, 4 and so on.
04:09Backward indexing is done as minus 1, minus 2, minus 3, minus 4 and so on.
04:16Accessing of list elements are done similar to string through indexing.
04:22Let us see how lists are similar to strings.
04:25Lists are similar to strings in following ways.
04:30Lend function returns the number of items in the list.
04:34Indexing can be used to return the item at a particular index.
04:38Slicing can be done to return a new list.
04:41Membership operators, in and not in, works on lists.
04:47That is, in tells if an element is present in the list or not and not in does the opposite.
04:55Concatenation and replication operators can be used with the lists.
04:59The concatenation operator adds one list to the end of another.
05:03The replication operator repeats a list.
05:07Let us see an example.
05:08Individual elements of a list are accessed through their indices.
05:16Let us see an example.
05:21If a negative index is passed while accessing list elements,
05:26Python adds the length of the list to the index to get elements forward index.
05:31A list L has five elements.
05:34Find the value of L minus 3.
05:36The index returned by the Python is minus 3 plus 5, that is, 2.
05:42The value of second index is returned.
05:46Although lists are similar to strings in many ways,
05:50yet there is an important difference.
05:53Strings are not mutable, while lists are mutable.
05:57Differences between lists and strings are listed here.
06:00Traversing a list means accessing and processing each element in a list.
06:07Sometimes, we need to access every element in a sequence.
06:11Basic ways to traverse a list in the Python are using for loop.
06:15Using the for loop, we go through the list one by one and print the current element on the screen.
06:23Range function is used to generate indices from 0 to len minus 1.
06:28With each iteration, I gets the index of next element and values of list are printed.
06:34Using while loop, with the help of the counter value and length of the list,
06:41we go through the list and print each element to the terminal.
06:45Now, here is the time to test your knowledge.
06:49Select the correct option for the given question.
06:51A list can be compared by using standard comparison operators of Python.
06:58The final result is in the form of true or false.
07:02Let us understand it with the help of some examples.
07:07Python raises error if two lists, L1 and L2, are not of comparable types.
07:13The corresponding elements at index 1 of L1 and L2 are not of same type.
07:20That is, in L1, index 1 holds number type and in L2, index 1 holds list type.
07:29List operations
07:30Joining, replicating and slicing lists are the most common operations performed with lists.
07:38The concatenation operator is used to join two or more lists.
07:43The concatenation operator, when used with lists, requires that both the operands must be of list types.
07:51We cannot add a number or any other value to a list.
07:55Some of the examples are shown here, which will result into error.
07:59List plus number
08:00List plus complex number
08:02List plus string
08:03Using repeating operator, we can replicate a list.
08:08To replicate a list specified number of times, suffix the digit after the operator.
08:16List slicing is a process that extracts certain elements from a list and forms them into another list.
08:23The syntax for list slicing is as follows.
08:26All three are optional.
08:28By default, slice starts from 0 and ends at n-1th position.
08:35If the stop index is not given, sequence will take it to end.
08:40Default value of step is 1.
08:43Now, here is the time to test your knowledge.
08:46Python raises an index error exception if the resulting index is outside the list.
08:55Let's do a quick recap.
08:57List is a mutable ordered sequence of objects.
09:01We can access elements of a list through the index.
09:05Lists index their elements just like strings.
09:09That is, two-way indexing.
09:10Lists are similar to strings like indexing, slicing, and accessing individual elements by using loop.
09:19LEN function returns the number of items in the list.
09:24Suppose M1 is a list containing the marks.
09:27Evaluate the given expressions.
09:30Suppose M1 is a list containing the marks.
09:34Evaluate the given expressions.
09:40Evaluate the They學ic Slide
09:52Evaluate the tutorial for the teach theoretical figure heavys self-explexes are discussed.
09:56Let's take a look.
09:57Evaluate the pendant.
09:58Evaluate the treatment danach for eachiße.
10:03There is also a list containing the checkish androk 두� €5 exposure.
10:06Evaluate the treatment of the 지� Nabandoumkar.
10:08Evaluate the treatment of the archipel Pay and Ver carte