Lists Part- 1

NPTEL-NOC IITM
1 Sept 201906:48

Summary

TLDRThis lecture introduces the concept of lists as an ordered data structure, allowing elements of different data types. It demonstrates creating lists with employee ID and names, and combining them into an employee list. The script explains positive and negative indexing in Python, showcasing how to access both top-level and sub-level components within a list, providing a clear guide to list manipulation and access.

Takeaways

  • 📚 Lists are a data structure consisting of an ordered collection of objects.
  • 🔢 Objects in a list, also called elements or components, can be of different data types.
  • 🔲 Elements inside a list are enclosed between square brackets.
  • 📝 Example lists include employee ID and employee name, with values separated by commas.
  • 👥 An employee list can be created using employee ID, employee name, and the number of employees.
  • 📋 Lists in Python can be viewed using the print function.
  • ➡️ Positive indexing starts from the leftmost element with an index of 0 and goes up to n-1.
  • ⬅️ Negative indexing starts from the rightmost element with an index of -1 and continues backwards.
  • 🔍 Accessing components in a list can be done using single slicing for top-level components and double slicing for sub-level components.
  • 🗂️ Example commands for accessing elements include using indices within square brackets.

Q & A

  • What is a list in the context of data structures?

    -A list is a data structure consisting of an ordered collection of objects, where the objects can be of different data types and are enclosed between square brackets.

  • How can elements in a list be referred to?

    -Elements in a list can be referred to as elements, objects, or components of the list.

  • What are the different data types that can be included in a list?

    -A list can include elements of various data types such as arrays, vectors, numeric matrices, logical values, complex vectors, etc.

  • How is a list created in Python?

    -A list in Python is created by enclosing a set of values within square brackets, separated by commas.

  • Can you provide an example of creating a list for employee IDs?

    -An example of creating a list for employee IDs would be using the list name 'id' with values [1, 2, 3, 4].

  • What is the purpose of creating a variable for the number of employees?

    -Creating a variable for the number of employees helps in keeping track of the total count of employees and can be used for various operations related to the employee list.

  • What command is used to view the contents of a list in Python?

    -The 'print' command is used in Python to view the contents of a list.

  • What are the two types of indexing in Python?

    -The two types of indexing in Python are positive indexing, which starts from the leftmost element with index 0, and negative indexing, which starts from the rightmost element with index -1.

  • How does positive indexing work in Python?

    -Positive indexing in Python starts from the leftmost element and the first index is 0, continuing up to n-1 where n is the length of the list.

  • How does negative indexing work in Python?

    -Negative indexing in Python starts from the rightmost element with an index of -1, and continues with decreasing values towards the leftmost element.

  • How can you access a specific element in a list using indexing?

    -To access a specific element in a list, you use the index of that element within square brackets following the list name. For example, 'list_name[index_value]'.

  • What is the difference between accessing top-level and sub-level components in a list?

    -Accessing top-level components in a list is done using a single set of square brackets with the index of the component. Sub-level components require an additional set of square brackets to specify the index within the nested structure.

  • Can you provide an example of accessing a sub-level component in a list?

    -An example of accessing a sub-level component would be accessing an employee's name from an employee list. If 'employee_list' is the top-level list and 'employee_name' is a sub-level list, you would access it as 'employee_list[1][1]' where 1 is the index of 'employee_name' and the second 1 is the index of the specific name within 'employee_name'.

  • How does the script summarize the key points covered about lists in Python?

    -The script summarizes by explaining how to create a list, the concept of positive and negative indexing in Python, and how to access both top-level and sub-level components within a list.

Outlines

00:00

📚 Introduction to Lists in Python

This paragraph introduces the lecture topic, focusing on lists in Python. It defines a list as an ordered collection of objects or elements, which can be of various data types such as arrays, vectors, numeric matrices, logical values, and complex vectors. The elements in a list are enclosed within square brackets. The paragraph also outlines the process of creating lists for employee IDs, names, and the number of employees, and demonstrates how to print these lists.

05:01

🔢 Understanding List Indexing

This section covers the concept of indexing in Python, including positive and negative indexing. Positive indexing starts from the leftmost element with an index of 0 and goes up to n-1. An example is provided using the employee names list. Negative indexing starts from the rightmost element with an index of -1 and goes backward. The same list is used to illustrate negative indexing. Both indexing types are explained with examples, demonstrating how to access elements using these indices.

🔍 Accessing List Components

This part explains how to access components of a list, distinguishing between top-level and sub-level components. To access top-level components, a single slicing operator with square brackets is used. For sub-level components, nested square brackets are employed. The paragraph provides specific examples, such as accessing an employee ID and the name 'Preethi' from the employee list, to illustrate the process of extracting both top-level and sub-level elements.

📑 Summary of List Operations

The final paragraph summarizes the key points covered in the lecture. It recaps the creation of lists, the two types of indexing (positive and negative), and the methods for accessing top-level and sub-level components in a list. The summary reinforces the main concepts and operations discussed, providing a concise review of the lecture content.

Mindmap

Keywords

💡List

A 'List' in the context of this video refers to a data structure that contains an ordered collection of objects, which can be of different data types. This is central to the video's theme as it demonstrates how to create and manipulate lists in Python. For instance, the script discusses creating a list for employee IDs and names, illustrating the flexibility of lists to hold various types of data.

💡Data Structure

A 'Data Structure' is a specialized format for organizing, processing, retrieving, and storing data. The video focuses on lists as one of the fundamental data structures, highlighting their ability to store multiple items in a sequential manner. This concept is foundational to understanding how data is managed in programming.

💡Indexing

In the video, 'Indexing' is the method used to access elements within a list by referring to their position. The script explains two types of indexing: positive and negative. Positive indexing starts from the left with index 0, while negative indexing counts from the right, starting with -1. This concept is crucial for navigating and retrieving specific elements within a list.

💡Positive Indexing

'Positive Indexing' is a method of accessing elements in a list starting from the first element at index 0 and increasing to the right. The video uses this concept to demonstrate how elements like 'Ram', 'Preethi', 'Sathish', and 'John' are accessed in the 'emp_name' list with index values of 0, 1, 2, and 3, respectively.

💡Negative Indexing

'Negative Indexing' is an alternative way to access elements from the end of a list, starting with -1 for the last element. The video script provides an example where 'John' has an index of -1, 'Sathish' -2, and so on, showcasing how to access elements from the right side of the list.

💡Elements

'Elements' in the context of lists are the individual items or components that the list contains. The video explains that these can be of various data types, such as numbers, strings, or even other lists. The script uses the employee ID and names as examples of elements within a list.

💡Accessing Components

The video discusses 'Accessing Components' as the process of retrieving specific parts of a list. It differentiates between accessing top-level components directly and sub-level components, which require nested indexing. For example, to access 'Preethi' from the 'employee_name' list, the script demonstrates using the index [1][1].

💡Employee ID

In the script, 'Employee ID' is used as an example of a top-level component within a list. It is a specific element that can be accessed using its index. The video creates a list with IDs like 1, 2, 3, 4 to demonstrate list creation and component access.

💡Employee Name

'Employee Name' is another top-level component in the list used in the video. It represents a list of strings, each corresponding to an employee's name. The script uses this to illustrate how to create a list of names and access individual names like 'Preethi' using indexing.

💡Number of Employees

'Number of Employees' is a variable in the script that holds the count of employees, which is also a component of the list. The video uses this to show how to incorporate numerical data into a list and access it using its index within the 'employee_list'.

Highlights

Introduction to the concept of Lists as an ordered collection of objects.

Explanation of how elements in a list can be of different data types.

Demonstration of creating a list with employee IDs using square brackets.

Creation of a list for employee names and the introduction of a variable for the number of employees.

Formation of an employee list combining ID, name, and count of employees.

Instruction on how to view the entire list using the print command.

Introduction to positive indexing in Python, starting from 0 for the first element.

Illustration of positive indexing with an example using employee names.

Introduction to negative indexing, starting from the last element with an index of -1.

Explanation of accessing top-level components in a list using single square brackets.

Guide on accessing sub-level components in a list using nested square brackets.

Example of extracting an ID from the employee list using indexing.

Method to extract a specific employee name from the list using nested indexing.

Technique for extracting a particular ID from the list of IDs within the employee list.

Summary of the lecture's key points on creating lists, indexing, and accessing components.

Thank you note to conclude the lecture.

Transcripts

play00:08

. Welcome to the lecture.

play00:15

In this lecture, we will see what is mean by Lists, how to create a list, indexing and

play00:23

also the accessing components of the list.

play00:27

First let us look what is mean by list?

play00:30

List is one of the data structure consisting of ordered collection of of the objects.

play00:36

Objects in a lists it can also be called as an elements or also components of the list.

play00:41

So, elements it need not be a same data type, it can also be a different data type.

play00:46

So, it can be of an array, vector, numeric matrix, logical value, a complex vector etcetera.

play00:54

Elements inside the list is enclosed between two square brackets.

play00:58

Let us see how to create a list.

play01:00

So, we will create the list for employee id and also for employee name and also we will

play01:08

create a variable that contains the number of employees.

play01:11

I am creating a list call id which has a set of values 1, 2, 3, 4 within inside the square

play01:18

bracket separated by commas.

play01:20

We will also create an employee underscore name as a lists, so which consists of values

play01:26

Ram, Preethi, Sathish and John which is separated by commas and also we will create a variable

play01:32

call number underscore employee which has a values of the number of employees.

play01:38

Now, let us see how to create an employee list using this employee id, employee name

play01:45

and also the number of employees.

play01:47

So, I will create a list employee underscore list which has values, id, employee underscore

play01:55

name and also the number of employees inside a square brackets.

play02:01

Let us see how to view the list; we will use the command; print of employee underscore

play02:08

list it gives us a list which has a values; the id values and also the employee names

play02:14

and also the number of employees under the employee underscore list.

play02:18

Now, let us see the indexing in python; there are two types of indexing one is positive

play02:24

and another one is negative indexing.

play02:27

So, first let us look at the positive indexing.

play02:30

So, it always starts from the left most element to the right most and 0 is the first index

play02:36

and it ranges till n minus 1.

play02:38

So, let us take an example; so we will considered the list emp underscore name which has a employee

play02:45

name which is Ram, Preethi, Sathish and John.

play02:47

Ram will have the index value of 0, Preethi will have the indexing value of 1, Sathish

play02:53

will have the indexing value of 2, John will have the indexing value of 3.

play02:58

So, if you run from the leftmost to right most; so that is a positive indexing.

play03:03

So, it always starts from 0 till n minus 1.

play03:07

Next we look at the negative indexing.

play03:10

So, negative indexing it starts from the rightmost element and minus 1; it will be the first

play03:17

index.

play03:18

So, we will again consider the same list which is a employee underscore name.

play03:23

So, again it has Ram, Preethi, Sathish and John.

play03:25

Now for the negative indexing, so it starts from the rightmost element John.

play03:30

So, John will have the index value of minus 1, Sathish will have the index value of minus

play03:35

2, Preethi will have the index value of minus 3, Ram will have the index value of minus

play03:41

4.

play03:42

So, this is the negative indexing; so it starts from the rightmost element minus 1 and it

play03:46

goes on.

play03:47

Now, let us see how to access a components of the list.

play03:51

So, there are two components inside the list; so, one is top level component and other one

play03:57

is sub level components.

play03:58

So, if you want it acts as a top level components in the lists.

play04:03

So, leave use single slicing operator, so which is will have a square brackets.

play04:08

So, if you wanted to access the sublevel components or the inner level components, use square

play04:13

brackets followed by the another square brackets.

play04:17

So, if you wanted to access id from the employee underscore list.

play04:20

So, our id is a top level component and employee underscore name is a top level component and

play04:28

also the number underscore employees is our top level components.

play04:32

So, if I wanted to extract id from the employee underscore list; so, the command is employee

play04:40

underscore lists followed by the index numbers.

play04:43

Since id is our top level component; so the it has a index value of 0.

play04:47

So, if you give employee underscore lists 0, so it will be able to displays elements

play04:52

in the id.

play04:53

So, if I wanted to extract Preethi from the employee name; so which belongs to the employee

play05:01

underscore list; so then print employee underscore list followed by the index value of the top

play05:09

level component and followed by the index value of the sublevel component.

play05:13

So, employee underscore names are top level component which has the index value of 1.

play05:18

The sub level component Preethi is in the sub level which is a index value of 1; so

play05:23

it is employee underscore list 1 of 1.

play05:26

Next, we will see how to extract the particular id from the id which belongs to the employee

play05:32

list.

play05:33

So, if you wanted to extract the second id then print employee underscore list.

play05:39

So, 0 is our top level component followed by the 1, which is our sub level component

play05:45

you will be able to extract the second id from the employee underscore lists.

play05:50

Now, let us summaries what we have covered in the structure.

play05:55

First, we saw how to create a list and we also saw indexing in python.

play06:01

So, there are two types of indexing one is positive indexing and another one is negative

play06:06

indexing.

play06:08

Positive indexing, it always starts from the left most element; so it has start with an

play06:12

index of 0.

play06:14

So, the negative indexing; it starts from the rightmost element with the index value

play06:18

of minus 1.

play06:19

We also saw how to access the top level component and also the sub level components in a list.

play06:26

Thank you .

Rate This

5.0 / 5 (0 votes)

Related Tags
Python ListsData StructuresList CreationIndexing BasicsElement AccessProgramming TutorialData TypesList OperationsCoding EducationScript Summary