Table of Contents

Sequence types are fundamental structures in Python used to store collections of data. Whether you're working with strings, lists, or tuples, sequences allow you to organize and access multiple values in a single variable. They form the basis for many programming tasks - from storing user input to processing items in a dataset or file. Understanding how sequences work unlocks many capabilities like iteration, indexing, slicing, and transformation. This page contains real-world practice problems that will help you become confident in manipulating sequences and using them effectively in your programs.

You’ll start with simple tasks like creating and accessing elements, then gradually move toward modifying sequences, performing calculations on them, and applying logical conditions.

To complete the exercises on this page successfully, you should first review the following foundational topics:

If you are not familiar with these concepts, we recommend reviewing the linked tutorials first. They provide the necessary background to tackle the exercises effectively.

Beginner Level Practice – Sequence Type in Python

At the beginner level, you will work with the basics of sequence types such as strings and lists. These exercises are designed to help you create sequences, access individual elements using indexing, and perform simple modifications. You will also practice combining sequences and checking for the existence of elements. The goal is to get comfortable with how Python stores ordered collections and how to interact with them safely and predictably. These tasks focus on clarity and logic, helping you understand one of the most essential data structures in Python programming.

Task 1: Favorite Colors List

Ask the user to enter three of their favorite colors, one at a time. Store the responses in a list and then display the full list back to them. Also, print the first and last color they entered. This task introduces basic list creation, indexing, and user interaction.

What this Task Teaches

You will learn how to collect user input multiple times and store values in a list. You'll also practice list indexing ([0] and [-1]) and printing values in a readable format. This reinforces core syntax and builds intuition around working with sequences.

Hints and Tips
  • Create an empty list first using [].
  • Use append() to add each color to the list.
  • To access the first and last items, use index 0 and -1.
  • Use print() to show both the full list and selected elements.
Common Mistakes
  1. Using incorrect list indexing: Some beginners confuse positions. Remember, Python indexing starts at 0, not 1. The last item can be accessed with index -1.
  2. Not converting input values properly: All values from input() are strings, but that’s fine here since colors are text. No need to cast.
  3. Replacing the list instead of appending: Avoid overwriting the list variable in each input. Use append() to build the list correctly.
  4. Printing the list raw: While print(my_list) works, it’s better to give the user readable output using custom messages.
  5. Forgetting to use brackets when accessing elements: You must use [] to access specific items in a list. list.0 is not valid Python.
Step-by-Step Thinking

Think like a user filling out a form. You're collecting their preferences and summarizing them. Follow the natural flow of asking, storing, and presenting.

  1. Create an empty list named colors
  2. Use a loop or repeated input statements to ask the user for 3 colors
  3. Append each input to the list using .append()
  4. After all inputs, print the full list
  5. Print the first color using colors[0] and the last using colors[-1]
How to Make it Harder
  • Ask the user how many colors they want to enter and use a loop instead of fixed inputs.
  • Sort the list alphabetically before displaying it.
  • Check if a specific color (e.g. “blue”) is in the list and print a special message if it is.

Task 2: Name Initials from a List

Ask the user to input the names of three people. Store them in a list. Then, display just the first letter (initial) of each name in uppercase, separated by spaces. For example, if the list is ["Alice", "bob", "Charlie"], the output should be: A B C.

What this Task Teaches

This task focuses on string indexing and list iteration. You’ll learn how to access the first character of each string and convert it to uppercase. This is useful when extracting structured data or abbreviations from text.

Hints and Tips
  • Use name[0] to get the first letter of each string.
  • Call .upper() to ensure it’s in capital form.
  • Use a for loop to iterate over the list.
  • Store initials in a new list or print them directly inside the loop.
Common Mistakes
  1. Forgetting to strip whitespace: If users input names with spaces, using .strip() ensures clean formatting before slicing the string.
  2. Indexing empty strings: If someone submits an empty name, name[0] will throw an error. Check that input is not empty.
  3. Using lower() instead of upper(): Make sure you explicitly capitalize each letter for proper formatting.
  4. Concatenating instead of printing with spacing: Beginners may print the initials without a space separator. Use end=" " in print() or build a list.
  5. Accessing wrong index: Remember, [0] gives the first character. Do not mistakenly use [1] or other indexes.
Step-by-Step Thinking

You want to reduce names to initials. That means accessing just one part of the string and repeating the action for each name in the list.

  1. Create an empty list called names
  2. Ask the user for 3 names and append() them to the list
  3. Loop through each name in the list
  4. Use name[0].upper() to get and format the initial
  5. Print all initials with spaces between them
How to Make it Harder
  • Ask for the full name (first and last), and print the initials of both parts.
  • Store initials in a new list and output them as a string.
  • Handle more than 3 names using a loop and dynamic input.

Intermediate Level Practice – Sequence Type in Python

At the intermediate level, working with sequence types requires more than just accessing individual elements. You'll now combine indexing, slicing, and built-in functions to transform and analyze data. These exercises will help you manipulate lists, extract sub-sequences, and perform calculations on grouped data. You’ll also explore how sequences behave when modified, how to update or remove items, and how to use conditional logic with sequence elements. Mastering these tasks builds a strong foundation for text parsing, data cleaning, and any operation involving structured collections. Precision, logic, and efficient use of Python’s capabilities will be your key tools in this section.

Task 1: Reverse a Shopping List

Ask the user to input 5 items they need to buy. Store the inputs in a list in the order they’re entered. Then display the full list and the same list in reverse order. This simulates reversing priority or display order of planned actions.

What this Task Teaches

You’ll reinforce your skills in collecting list data from user input and practice using slicing and built-in functions to reverse the order. It also teaches clear output formatting and understanding how sequence order affects program logic.

Hints and Tips
  • Use a loop or repeated input() calls to collect five items.
  • Store them in a list using append().
  • To reverse the list, use slicing like my_list[::-1] or reversed().
  • Print both the original and reversed lists with clear messages.
Common Mistakes
  1. Reversing the list in place accidentally: Using list.reverse() changes the original list. Use slicing if you want a reversed copy without modifying the original.
  2. Incorrect slicing syntax: Forgetting the step in my_list[::-1] will not reverse the list. Ensure you use the correct syntax with a negative step.
  3. Inconsistent output formatting: Displaying the reversed list without a label can confuse users. Always clearly separate your outputs.
  4. Modifying the list during iteration: Avoid manipulating the list while looping through it. It’s safer to reverse the list separately and store it in another variable.
  5. Using the wrong method like sort() instead of reverse(): Sorting alphabetically is not the same as reversing the order of input.
Step-by-Step Thinking

Think of the list as a timeline - you want to show what happens if that timeline is flipped. Maintain a clean structure of steps from input to output.

  1. Create an empty list named items
  2. Use a loop to ask the user for 5 shopping items
  3. Append each input to the items list
  4. Display the original list using print()
  5. Reverse the list using slicing or reversed()
  6. Display the reversed list with a clear label
How to Make it Harder
  • Let the user decide how many items to enter, and loop dynamically.
  • Check for duplicate entries before adding to the list.
  • Ask the user whether they want to sort or reverse the list before displaying it.

Task 2: Middle Slice Analyzer

Ask the user to enter a list of numbers separated by commas (e.g. 4, 7, 12, 3, 9, 1, 6). Convert this input into a list of integers. Then extract and display the middle three numbers from the list. If the list has fewer than three numbers, print an error message.

What this Task Teaches

You will practice splitting a string, type conversion, slicing a subrange, and performing input validation. This task builds skills related to data parsing and precise list manipulation - both crucial in data processing.

Hints and Tips
  • Use split(",") to turn the input string into a list of strings.
  • Convert each string to an int using a list comprehension.
  • Use len() to check the size of the list before slicing.
  • For odd-length lists, use middle index logic: mid = len(lst) // 2.
  • Slice using lst[mid - 1: mid + 2] to extract 3 values.
Common Mistakes
  1. Forgetting to strip whitespace: After splitting input, items like " 7" may cause errors. Use .strip() before converting to int.
  2. Incorrect slicing bounds: Always subtract 1 and add 2 to the middle index to get three values. Off-by-one errors are very common in slicing.
  3. Not handling short input lists: Failing to check list length can cause crashes. Always validate before slicing.
  4. Assuming user enters numbers correctly: If input includes non-numeric values or empty strings, you must prepare for ValueError during conversion.
  5. Hardcoding slice indices: Avoid using fixed indexes like [2:5]. Calculate the middle dynamically for flexibility.
Step-by-Step Thinking

You're parsing raw input, sanitizing it, and analyzing the center of the list. Think of it as zooming in on the core of the data.

  1. Ask the user to enter numbers separated by commas
  2. Split the string into a list using .split(",")
  3. Use .strip() and int() to convert each item to an integer
  4. Check that the list has at least 3 elements using len()
  5. Calculate the middle index using // division
  6. Slice out the middle three values using correct boundaries
  7. Display the sliced portion with a message
How to Make it Harder
  • Accept input until the user types “done” instead of all at once.
  • Allow both even and odd list lengths and define different slicing rules.
  • Return the sum or average of the middle values instead of just printing them.

Advanced Level Practice – Sequence Type in Python

At the advanced level, sequence manipulation moves beyond individual lists and simple access. You'll now apply conditional logic, nested sequences, and efficient iteration patterns to handle more complex data. These tasks challenge your understanding of structure, memory, and readability, requiring you to think in terms of transformations and abstraction. You’ll work with sequences of sequences (like lists of lists), filters, aggregations, and comprehensions. Real-world Python code often involves parsing semi-structured data or building dynamic lists based on conditions - this section reflects that. Think critically, write clean code, and always keep efficiency in mind as you solve these challenges.

Task 1: Filter Even Numbers from Nested Lists

You’re given a list of number lists, like [[3, 8], [12, 5], [6, 7, 9]]. Write a program that collects all even numbers from all sublists and creates one flat list of just the even values. Display the final result. The output for this input should be: [8, 12, 6].

What this Task Teaches

This exercise focuses on nested iteration and filtering using conditions. You'll get experience with nested loops or list comprehensions, boolean logic, and collecting results efficiently. It trains you to extract specific patterns from multi-dimensional data structures.

Hints and Tips
  • Use a nested for loop or list comprehension.
  • Check each number using num % 2 == 0 to find even values.
  • Use append() to collect them or build the list in one expression.
  • Make sure to flatten the result - don’t keep them in sublists.
  • Display the result with a clear label.
Common Mistakes
  1. Using a single loop: Beginners may try to iterate the outer list only, forgetting that each element is itself a list. You need a nested loop to reach each number.
  2. Appending sublists instead of numbers: If you use result.append(sublist), you'll end up with nested data. You should append individual even numbers.
  3. Incorrect even check: Some try num / 2 == 0 instead of using the modulus operator %.
  4. Using list comprehension without flattening: It’s common to write [[x for x in sub if x % 2 == 0] for sub in lst], which results in nested lists. Use a flattened version instead.
  5. Forgetting to initialize the result list: Always define the result list before appending to it; otherwise, you’ll get a NameError.
Step-by-Step Thinking

Think of the task like scanning through folders inside folders - you're pulling out specific items that match your rule.

  1. Create an empty list named even_numbers
  2. Loop over each sublist in the main list
  3. Loop over each number inside the sublist
  4. Check if the number is even using % 2 == 0
  5. If it is, append it to even_numbers
  6. Print the result once all numbers have been checked
How to Make it Harder
  • Write the same logic using a single list comprehension
  • Sort the final list before printing
  • Count how many even numbers were found and display that too

Task 2: Grouping Names by First Letter

Ask the user to input 6 names. Group these names into a dictionary where each key is the first letter (in uppercase), and the value is a list of names that start with that letter. Display the resulting dictionary. For example, inputting Alice, Alan, Bob, Amy, Ben, Clara would result in:


      {
        'A': ['Alice', 'Alan', 'Amy'],
        'B': ['Bob', 'Ben'],
        'C': ['Clara']
      }
    
What this Task Teaches

This exercise introduces dictionary-based grouping, dynamic key creation, and case handling. It also builds your ability to construct and update compound data structures programmatically - a skill used in categorization, filtering, and grouping operations.

Hints and Tips
  • Initialize an empty dictionary before the loop.
  • Use name[0].upper() to get the first letter.
  • Check if the key exists using if key not in dict.
  • If it doesn’t exist, create a new list for it.
  • Always append the name to the correct key’s list.
Common Mistakes
  1. Using lowercase letters as keys inconsistently: If you don’t normalize the case using upper(), you might end up with separate keys for ‘a’ and ‘A’.
  2. Forgetting to check key existence: Directly appending to a key that doesn’t exist causes a KeyError. Always check with if not in first.
  3. Mixing list and dictionary logic: Ensure that values are lists, not strings or other types - otherwise, the program may behave unpredictably.
  4. Not stripping whitespace from names: User input can have extra spaces. Always .strip() names before processing.
  5. Using += with strings instead of append() with lists: These are not interchangeable and will lead to incorrect structures.
Step-by-Step Thinking

Think of this task like sorting files into folders by their first letter. The folder (key) must exist before adding a file (name) into it.

  1. Ask the user for 6 names using a loop
  2. For each name, extract the first letter and convert it to uppercase
  3. Check if the letter already exists as a key in the dictionary
  4. If not, create a new list under that key
  5. Append the name to the correct list
  6. Display the final dictionary
How to Make it Harder
  • Allow unlimited names until the user types “done”
  • Sort the names in each list alphabetically
  • Print the dictionary sorted by keys

Author of This Exercises

Marcus Johnson

Marcus Johnson

I've been working as a Senior Python Backend Developer at the company StreamFlow Analytics...

Learn more →

Author of This Exercises

Sarah Jane Notli

Sarah Jane Notli

I'm a Senior Data Scientist at a Leading Tech Company in Silicon Valley...

Learn more →