Python List

Python Lists are one of the most commonly used advanced data structures in Python, and they are used to store a collection of items. Lists are ordered, mutable, and can contain elements of different data types. Lists are defined by enclosing a comma-separated sequence of values in square brackets.
ex: Lets create a list of employee names; emp_names=[‘John’,’Alex’,’Peter’,’Marsh’] where emp_name is the name of the list and John, Alex, Peter, Marsh are the 4 elements of this list.

In this blog, we will explore the basics of working with lists in Python.

Creating a list:

It is created by placing elements/items inside [ ], separated by commas.

For example:

  • subject= [‘Python’, ‘C++’, ‘HTML’, ‘CSS’] # list with string values
  • marks = [98, 67,89,75,53] # list with integer values
  • mixed=[‘Apple’,10,’Orange’,20.5] #This is an example of list containing different datatypes
  • empty_lst=[ ] # This is an example of empty list.

Accessing an element of a list:

Each item/element in a list is associated with a number. The number is known as a list index. We can access elements of a list using the index number (0, 1, 2 … or ….. -3,-2,-1).

➡️Forward or Positive Indexing:

We can access elements of a list using the index number (0, 1, 2 …). Range of index is from 0 to n-1 where n is the total number of items in the list. For example,

  • marks = [98, 67,89,75,53] # list with integer values
  • It is a list with 5 items, means n=5 so the range of index is from 0 to 4 (0 to n-1, where n is 5)
  • print(marks[0]) #Output: 98
  • print(marks[1]) #Output: 67

In the above example we have created a list named marks, and we can also see that each element in the list is associated with an index value, which can be used in order to access that element.

Note: The list index always starts with 0. Hence, the first element of a list is present at index 0, not 1.

➡️Backward/Reverse or Negative Indexing:

We can access elements of a list using the index number (….-3,-2,-1). For example,

  • marks = [98, 67,89,75,53] # list with integer values
  • It is a list with 5 items, means n=5 so the range of index is from -5 to -1 (-n to -1, where n is 5)
  • print(marks[-1]) # Output: 53
  • print(marks[-5]) # Output: 98

In the above example we have created a list named marks, and we can also see that each element in the list is associated with an index value, which can be used in order to access that element.

Note: The list index always starts with -n. Hence, the first element of a list is present at index -n, and the last element of the list is at index -1

For Detailed information, Join our Self Learning program on Python programming designed by experts.

Contact :

Hexnbit Edtech Private Limited

B-309, B Tower, The iThum, Sector 62, Noida-

Email: info@hexnbit.com

Website : https://www.hexnbit.com/

+91-9818894299

+91-9311124664

[wp_call_button btn_text="Call Now" btn_color="#269041" hide_phone_icon="yes"]

Notice

Dear All,
The site is getting upgraded so there would be login issues for today 14th September 2023. No issues in enrolling to new courses and making payments. Its totally secure.