VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/04/python-list-programs-for-absolute-beginners/

⇱ Python List |Python List Programs For Absolute Beginners


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

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

Python List Programs For Absolute Beginners

Rahul Shah Last Updated : 21 Oct, 2024
5 min read
This article was published as a part of the Data Science Blogathon.

Python Lists Refresher

The list is one of the most widely used data types in Python. A Python List can be easily identified by square brackets [ ]. Lists are used to store the data items where each data item is separated by a comma (,). A Python List can have data items of any data type, be it an integer type or a boolean type.

One of the leading reasons why lists are being widely used is that Lists are mutable. Being mutable means, any data item of a List can be replaced by any other data item. This makes Lists different from Tuples, which are also used for storing data items but are immutable.

For example.,

a = ['Hello', 1, 4.63, True, "World", 2.0, "False"]

Indexing in a List are of two types:

1. Positive Indexing – Here the indexing starts from 0, moving from left to right.

2. Negative Indexing – In this, the indexing starts from right to left and the rightmost element has an index value of -1.

Taking the above example as reference, Positive and Negative Indexing will look like:

πŸ‘ Python list image

Based on indices of elements, we can also perform Slicing on Lists.

For example, taking our earlier defined List a, 

a[2 : 5] gives [4.63, True, "World"]
a[ : ] gives ["Hello", 1, 4.63, True, "World", 2.0, "False"]
a[-1: -3 : -1] gives ["False", 2.0]

Similarly, can Replace our existing data item with a new value.

For example, our list a:

a[1] = 2
print(a) gives ['Hello', 2, 4.63, True, "World", 2.0, "False"]

Apart from these operations, Python List has many functions to fulfill anyone’s need. Following are the popular yet never getting old Python Programs on List that will help to build the logical mind of a beginner.

Python List Programs Every Beginner Should Know

1. Program to check if the Given List is in Ascending Order or Not

Python Code:
list1 = [1, 2, 3, 5, 4, 8, 7, 9]
temp_list = list1[:]
list1.sort()
if temp_list == list1:
 print("Given List is in Ascending Order")
else:
 print("Given List is not in Ascending Order")

Explanation: Given a list list1 having 8 int values. This list list1 is assigned to another variable temp_list. Since we are using the .sort() function of the list, it will sort our list list1 which means it will arrange our list1 data items in ascending order.

Thus our original list needed to be stored somewhere. Now our temp_list has an earlier version of list list1 while list1 has become the sorted version of list1. Now apply, if the condition on temp_list and l1. If temp_list is equal to l1, this means that our list was already sorted. If this condition is fulfilled, print Given List is in Ascending Order else print Given List is not in Ascending Order.

2. Program to Find Even Numbers From a List

list2 = [2, 3, 7, 5, 10, 17, 12, 4, 1, 13]
for i in list2:
 if i % 2 == 0:
 print(i)
'''
Expected Output:
2
10
12
4
'''

Explanation: Given a list list2 having 10 int values. We need to find even numbers from the given list. Use for loop to iterate over the list. And for every iteration, use the if condition to check if the list data item at a particular iteration is completely divisible by 2, which means i % 2 should be equal to zero. Now, every item fulfilling this condition will get printed.

 

3. Program to Merge Two Lists

list3 = [1, 2, 4, 6]
list4 = [9, 3, 19, 7]
list3.extend(list4)
print(list3)
'''
Expected Output:
[1, 2, 4, 6, 9, 3, 19, 7]
'''

Explanation: Given two lists list3 and list4 having certain numerical values. Use the .extend() function of the Python List to merge both lists. Thus, Merging these lists will give a single list having items from the first list followed by the second list. Now print the list3, which will have elements from both the lists list3 and list4. If you want the second list list4 items to be printed first followed by list3 data items, use list4.extend(list3), then print the list4.

4. Interchange First and Last Element of a List

list5 = [1, 29, 51, 9, 17, 6, 7, 23]
list5[0], list5[-1] = list5[-1], list5[0]
print(list5)
'''
Expected Output:
[23, 29, 51, 9, 17, 6, 7, 1]
'''

Explanation: Given a list list5 having certain values. As discussed above, Lists are immutable, thus any data item can be replaced with another value. Use indexing to replace the value. Replace the 0th index with -1st index of list5 and -1st index with 0th index of list5 simultaneously. Now print the updated list list5.

 

5. Program to Subtract a List from Another List

a = [1, 2, 3, 5]
b = [1, 2]
l1 = []
for i in a:
 if i not in b:
 l1.append(i)
print(l1)
'''
Expected Output:
[3, 5]
'''

Explanation: Given 2 lists a and b having certain values. To subtract the second list from the first list, we can get those list items from the first list a which are not present in the second list b. Create an empty list l1. Use for loop over the list a, and for every iteration, use if condition along with not in to check if i representing the data item of the list a for that current iteration is not present in list b. If i is not present in list b, append i for that iteration to an empty list l1 created at the beginning. In the end, you will have a populated list l1 having those items exclusive to list a.

6. Program to Get Data Items From a List Appearing Odd Number of Times

x = [1,2,3,4,5,1,3,3,4]
l1 = []
for i in x:
 if x.count(i) % 2 != 0:
 if i not in l1:
 l1.append(i)
print(l1)
'''
Expected Output:
[2, 3, 5]
'''

Explanation: Given a list x having certain values. Create an empty list l1. Use for loop over the list x, and during every iteration, use if condition with .count() function of Python Lists to check the number of occurrences of i in list x where i is representing the data item from the list x at every iteration. Use a nested if condition to select distinct data items from list x having odd occurrences. Now, append i to an empty list l1 created at the beginning. In the end, print the list l1.

 

Conclusion

Thus, Lists can be used in scenarios where you want to store your values temporarily or to store them longer. Lists are one of Python’s built-in datatypes apart from Tuples, Dictionaries, and Sets.

Each of the mentioned Python Programs can be done more efficiently. The Modus Operandi of solving these Python Programs has been done by keeping the beginner’s mind in consideration. For example, one can use List Comprehensions wherever possible to make the program shorter. Also, can use user-defined functions to increase the versatility of code.

Each program is enclosed with an Expected Output which would come if the given inputs are given. Along with the expected output, an Explanation of the program is given to let the beginner understand the program in the best possible way.

Apart from this, any beginner can find more Lists programs on the internet. Internet is abundant with the resources needed to learn anything. Getting perfection in Lists will help one in any domain, whether in Machine Learning or Web Development in Python.

Although Lists is a wide topic and require practice to excel, the mentioned programs will help any beginner to build the logic on how to use Loops and Conditions along with List functions.

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion. 

IT Engineering Graduate currently pursuing Post Graduate Diploma in Data Science.

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

David Read

In item 4, the explanation begins: "Explanation: Given a list list5 having certain values. As discussed above, Lists are immutable..." I think you mean "mutable" since lists can have their values changed.

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