VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/04/understanding-the-concept-of-list/

⇱ List | What Type of a Data Structure is List | List for Data Science


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Understanding the Concept of List

Shipra Saxena Last Updated : 06 Apr, 2021
5 min read

Objective

  • The List is an integral part of python when we talk about data structure.
  • Understand how to perform different operations over a list.

Introduction

Let’s imagine a scenario, we are required to store marks of all the students in class 10th roll number wise. One way to solve this problem is to use variables for all the students and store their marks. As shown below.

Note: If you are more interested in learning concepts in an Audio-Visual format, We have this entire article explained in the video below. If not, you may continue reading.

πŸ‘ List image

But this is not an efficient way to solve our problem as

  • The number of variables becomes very large as the number of students increase. This can lead to large memory overhead.
  • We will not be able to analyze the data for answering some questions like what are the average marks of the last five students? We can not answer these questions since variables do not inherently support sequence.

So we need a data structure that supports sequences and helps in questions related to data. This is where the list comes in.

In this article, we will be talking about the list. Let’s see the topics we will be covering.

  1. What is a List?
  2. Sub-setting
  3. Adding elements to an existing list
  4. Deleting elements
  5. Looping over a list to access its elements.

Let’s now discuss each of them in detail.

What is a List?

A list is an ordered data structure with elements separated by a comma and enclosed within square brackets. For example, list1 and list2 shown below contains a single type of data.

πŸ‘ List 1 and 2

Here, list1 has integers while list2 has strings. Lists can also store mixed data types as shown in the list3 here.

πŸ‘ list 3

Subsetting

Now let’s see how can we access the elements of a list. Before we subset it, let’s see how the elements are stored in it. Take the example of the list, its elements are stored using indexes starting from 0.

πŸ‘ subsetting the list

Here, element 1 stands at 0, and element 2 stands at index 1, and so on.

If you wish to extract a single element, say at index 1 we can use the square bracket along with the index number. So, list3[1] will give the β€œPython”, which is present at index 1. Like this

πŸ‘ extract a single element in list

This was how we can extract a single element from a list, what if, we want to extract a sequence out of it. We can do this using the following format, where within a square bracket we can give a range separated by a colon.

πŸ‘ extract sequence list

If I give, list3[1:4], it will start from index 1 and reaches up to one index before 4. That means the list starts from 1 and stops at 3 it doesn’t include 4. Similarly, if you give list3[2:5] it will return a list of elements in the list3 from index 2 to 4.

Negative indices are another interesting concept. Suppose, you want to access the last element from it, you can use list3[-1]. It will give you the last element in the list which is Awesome in this case.

Adding Elements to an Existing List

Now let’s explore how can we add an element to an existing list. A single element can be added using an append function. As shown below.

πŸ‘ Adding Elements to an Existing List

In this example using append, we have added element 4 at the end.

We can also add multiple elements to the list, for this we use extend function. The extend function accepts the multiple elements, in comparison to append which accepts a number i.e a single element. Look at the example below-

πŸ‘ add multiple elements

Here, the function will extract each element from the input list and add it to the main list which is list3 in this case.

We can also add a list to the existing one. For this, we can use the append function as discussed previously. The only difference is instead of a number we will pass lists as input. It will add the whole input list as an element.

πŸ‘ add list to a list

So, in this example, the input list [7,8] is added at index 6th in the list3. Also, it can be accessed using the index value.

Deleting elements

Let’s see how can we remove elements from a given list. Suppose you want to remove an element by its value. In this case, we can use the remove function and giving the element that we want to remove as an input. For example, if we want to remove 2 as shown below, we can use the code list3.remove(2). when you print the list there will be no 2 present in it.

πŸ‘ Image

The second method is when we use the index value to remove the element from it. In such a case, we will use the del keyword. Suppose I want to remove the element present at index 3, I will use del list3[3]. As shown in the following image

πŸ‘ Image

Looping over The List

Let’s see how one can iterate over each element of a list for performing a task such as printing. Here is an example of how we can iterate over it using a For loop to print the elements.

πŸ‘ Image

The For loop will extract each element and print it. This is how we can get access to each element using a β€˜for’ loop.

End Notes

This was all about Lists in this article. Here, we saw what it is, how to access elements from it. Also, we saw how to add and delete elements from it along with that how to iterate over them to process it further.

If you are looking to kick start your Data Science Journey and want every topic under one roof, your search stops here. Check out Analytics Vidhya’s Certified AI & ML BlackBelt Plus Program

If you have any queries let me know in the comment section!

Shipra is a Data Science enthusiast, Exploring Machine learning and Deep learning algorithms. She is also interested in Big data technologies. She believes learning is a continuous process so keep moving.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner