List Data Structure in Python

LEELA S
3 min readMar 3, 2022

Introduction

Lists are the simplest data structure in python and are used to store a set of values.It is an ordered sequence of any datatype(string,float,integer etc..).Values in the list are called items (or) elements.These are mutable which means changeable either adding or removing elements in the list.

To create a list,define a variable to contain an ordered set of items separated by a comma.Square bracket[] is used to enclose the items.

Characteristics of list

  • The lists are ordered
  • The element of the list can access by index
  • The lists are mutable(changable) types
  • A list can store the number of various elements

Syntax

Example

#Accessing elements on a list

The different ways to access list on the elements are Indexing,Slicing list,Concatenation and Repeating list,Testing for membership on a list

a)Indexing

we can access elements in the list with the help of index operator[]. The index value starts from zero and goes to length -1(n-1).

The first element of the list is stored in 0th index and 1st element is stored in first index and so on.

Example:

Output:

i)Nested Indexing

Nested lists are list objects,where the elements the elements in the list can be lists themselves.It is possible to access a nested list by using nested indexing.

Example:

Output:

ii)Negative Indexing

Python supports negative indexing for sequence type like lists.The last item on the list takes the -1 index,the second to the last item has the -2 index and so on.

Example:

Output:

b)Slicing

The value stored in a list can be accessed using slicing operator , the colon(:) with indexes starting at 0 in the beginning of the list and ending with -1.

It is used to access a range of elements on lists.

Syntax:

Example:

Output:

iii)Concatenating and Repeating list

In concatenating, it is possible to combine or concatenate two or more lists with the (+) operator.

In Repeating list,we can use the (*) operator and a number to specify the number of items the list to be repeated.

Example:

Output:

iv)Testing for membership on a list

Membership operator used to test, if an object is stored on a list or not.There are two types of membership operator: “in” and “notin”.

Python returns either True or False after evaluating the expression.

Example:

Output:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

LEELA S
LEELA S

No responses yet

Write a response