codingstreets
Search
Close this search box.

Python List Data Type Project for Beginners: Unlocking Python Lists

Python-List-Data-Type
Credit: Canva

Overview

Python List Data Type is one of the built-in data types in Python. Other Python data types are  

Tuple, Set, and Dictionary. Python List is used to make a collection of data. Since Python List is mutable (element can be changed) therefore, the List Method supports append(), remove(), index(), and count() functions etc.

What’s Next!

In this Python List Data Type Project, we will look at a couple of Python List beginner problems. From the concept of Python List to the Python List method, we will walk you through all by solving the questions.

Table of Contents

Project 1: To-Do List

Create a program that allows users to maintain a to-do list. The program should allow users to add tasks, remove tasks, mark tasks as completed, and display the current list.

				
					def display_menu():
    print()
    print("Welcome to the Task!")
    print("1. Add Task")
    print("2. View Task")
    print("3. Remove Task")
    print("4. Exit")
    
def add_task(tasks):
    task = input("Enter task: ")
    tasks.append(task)
    print("Task added successfully!")
    print()

def view_task(tasks):
    if tasks:
        print("\nCurrent Tasks:")
        for number, task in enumerate(tasks,1):
            print(f"{number}. {task}")
    else:
        print("\nNo tasks available.")
        print()
        

def remove_task(tasks):
    if tasks:
        view_task(tasks)
        task_number = int(input("Enter the task number to remove: "))
        if task_number <= len(tasks):
            del tasks[task_number - 1]
            print()
            print("Task is deleted.")
        else:
            print("Invalid task number!")
    else:
        print("No task to remove.")
        
def exit():
    print("Thank you for using the Task!")

tasks = []

while True:
    display_menu()
    choice = input("Enter your choice: ")
    if choice == '1':
        add_task(tasks)
    elif choice == '2':
        view_task(tasks)
    elif choice == '3':
        remove_task(tasks)
    elif choice == '4':
        print()
        print("Program is exit now.")
        exit()
        break
    else:
        print("Invalid choice! Please try again.")
				
			

Explanation: The program starts with calling the function display_menu() which displays the menu list of the program instructions.

Now, we create a function add_task() and pass a parameter tasks, which is a data type of list. This function is used to add the task by using the input() function. Once the task is asked by the user, the task is added to the tasks list referred to as the parameter value.

Next, we create a function view_task() and pass the parameter tasks. So, by using the conditional statement, we check if we have a task remaining, so we use Python for loop along with the enumerate() function (enumerate is used to list the items along with the numbers) and display the task with the print() function, otherwise displays no task is available.

Now we create a function remove_task() and pass the parameter tasks. Here we used the nested if conditional statements. First, we started by checking if the task was available to display, if not, then no task to remove is displayed. So we displayed all current tasks, and next asked the user which task to remove by using the input() function.

Inside the first if conditional statement, we used now another if statement, where we put a condition that if the task number by the user is <= the total number of tasks available in the tasks list, then the task is deleted else the task number is invalid.

Now we create a function exit() to get out of the program.

Last, we used the Python while loop and called the display_menu() so that every time it can show the menu list to the user. As per the menu instruction, we give the user to choose the option by using the if conditional statement and execute the program.

Project 2: Shopping List

Develop a program that helps users maintain a shopping list. The program should allow users to add items to the list, remove items, and display the current list of items.

				
					shopping_list = []

def shopping_menu():
    print()
    print("1. Add item")
    print("2. Remove item")
    print("3. Display item")
    print("4. Exit")
    print()
    
def add_items(shopping_list):
    item = input("Enter item name: ")
    shopping_list.append(item)
    print("Item added to list")
    
def remove_item(shopping_list):
    item = input("Enter item name to remove: ")
    if item in shopping_list:
        shopping_list.remove(item)
        print("Item removed from list")
    else:
        print("Item not found in list")

def display_shopping_item(shopping_list):
    print("Your shopping list:")
    if shopping_list:
        for id, item in enumerate(shopping_list,1):
            print(f'{id}.{item}')
    else:
        print("Your shopping list is empty")

def exit():
    print("Thank you for using the shopping list")

while True:
    shopping_menu()
    choice = input('Choose an option: ')
    if choice == '1':
        add_items(shopping_list)
    elif choice == '2':
        remove_item(shopping_list)
    elif choice == '3':
        display_shopping_item(shopping_list)
    elif choice == '4':
        exit()
        break
    else:
        print("Invalid option")
				
			

Explanation: The program starts with calling the function shopping_menu() which displays the menu list of the program instructions.

Now, we create a function add_item() and pass a parameter shopping_list, which is a data type of list. This function is used to add the items by using the input() function. Once the item is asked by the user, it is added to the shopping_list referred to as the parameter value.

Now we create a function remove_item() and pass the parameter shopping_list. First, we started by asking for the item to be removed. With the condition statement, we accessed each item from the shopping_list and applied the remove() function. If the item mentioned by the user is present in the shopping_list then the item is removed otherwise no item is found displayed.

Next, we create a function display_shopping_item() and pass the parameter shopping_list. So, by using the conditional statement, we check if we have an item remaining, so we use Python for loop along with the enumerate() function (enumerate is used to list the items along with the numbers) and display the item with the print() function, otherwise displays no item is available.

Now we create a function exit() to get out of the program.

Last, we used the Python while loop and called the shopping_item() so that every time it can show the shopping menu list to the user. As per the menu instruction, we give the user to choose the option by using the if conditional statement and execute the program.

Project 3: Word Frequency Counter

Write a program that analyzes a piece of text and counts the frequency of each word. The program should store the word frequencies in a list of tuples, where each tuple contains a word and its frequency.

				
					texts = 'This is a sample text This is a sample text'
words = texts.split()
print(words)
word_frequency = {}
for word in words:
    word_frequency[word] = word_frequency.get(word,0)+1
    word_freq_list = [(word,freq) for word,freq in word_frequency.items()]
print(word_freq_list)
				
			

Explanation: The program starts with a random set of texts. Next, each text is split by using the split() function. Declared a variable word_frequency with the type of dictionary to store the frequency of each word. Next, use the for loop to access each text from the variable words.

Now, we access each word one by one and check if the word is already present in the word_frequency, if the word is found the counting increments by 1 and if not, the word is added to the count of 1.

Next, we create a list of tuples from the word_frequency dictionary, where each tuple contains a word and its frequency.

Next, we print the word frequency, each word along with the frequency of the word.

Project 4: Number Sorter

Create a program that takes a list of numbers as input and sorts the numbers in ascending order. The program should use a sorting algorithm (e.g., bubble sort, selection sort) to sort the numbers.

				
					numbers = [64, 34, 25, 12, 22, 11, 90]
n = len(numbers)
for i in range(n):
    for x in range(0, n-i-1): #0 7-0-1
        if numbers[x] > numbers[x+1]:  #6 > 7
            numbers[x], numbers[x+1] = numbers[x+1], numbers[x]
            
print(numbers)
				
			

Explanation: The program starts with a list of numbers which is counted by using the len() function. Next, we loop through the list of numbers. The inner loop iterates to the point where the number starts to sort. It gradually reduces the number of comparisons as the largest number is sorted to the end of the list.

n-i-1 ensures that the inner loop does not compare already sorted numbers at the end of the list.

Next, we used the conditional statement to check if the current number is greater than the next number, if it is, then the numbers are swapped together to transfer the largest number to the behind position in the list.

Finally, the sorted list is displayed by using the print() function.

Project 5: Palindrome Detector

Develop a program that checks if a given list of words is a palindrome. The program should compare the list of words with its reverse to determine if it is a palindrome.

				
					def palindromeString(word):
    reverse_String = word[::-1]
    return reverse_String

word = ["hello", "world", "world", "hello"]
reverse_String = palindromeString(word)

if word == reverse_String:
    print(word,'is a Palindrome String.')
else:
    print(word,'is not a Palindrome String.')
				
			

Explanation: The program starts with a function called palindromeString() with an assigned parameter word. With Python slicing [:: -1], we access each word in the reverse pattern, i.e., end to start and return the reverse_String.

So, we defined a variable that stored the list of words and passed the variable to the parameter value. Finally, use the conditional statement to compare the original words and the reversed words, and display the statement as per the condition is met.

Let’s take another example: Instead of a word, check if the character is Palindrome.

				
					palindromeString = ['m','a','d','a','m']
copyList = palindromeString.copy()
copyList.reverse()

if palindromeString == copyList:
    print(palindromeString,'is a Palindrome String.')
else:
    print(palindromeString,'is not a Palindrome String.')
				
			

Explanation: The program starts with the character of the word to check if the reverse character is Palindrome. Next, we copied the character and applied the reverse method to access each character in a reverse pattern.

Finally, we compare the palindromeString with copyList to check if the character is Palindrome. With the print() function, we display the statement matched according to the conditional statement.

Recent Articles