Skip to ContentGo to accessibility page
Introduction to Python Programming

12.3 Recursion with strings and lists

Introduction to Python Programming12.3 Recursion with strings and lists

12.3 Recursion with strings and lists

Learning objectives

By the end of this section you should be able to

  • Demonstrate the use of recursion to solve a string problem.
  • Demonstrate the use of recursion to solve a list problem.
  • Use the built-in count() list function.

Recursion with strings

A word that is spelled the same forward and backward is called a palindrome. Ex: racecar.

Recursion can be used to identify whether a given word is a palindrome.

Checkpoint

Identifying a palindrome

Identifying a palindrome; ch 12, video 5

Concepts in Practice

Recursion with strings

Refer to the animation above.

1.
Which of the following does palindrome() recognize as a palindrome?
  1. madamm
  2. madaM
  3. madame
2.
What would happen if the condition on line 4 of palindrome() is changed to if len(word) == 1:
  1. Only palindromes with an odd number of letters would be recognized correctly.
  2. Nothing. The function would work the same.
  3. The function would not recognize any palindromes.
3.
What would happen if the condition in line 8 palindrome() is changed to if palindrome(word.strip(word[0])).
  1. Nothing. The function would work the same: the first and last letter would be removed.
  2. Some words, such as "madamm", would be incorrectly recognized as a palindrome.

Recursion with lists

The animation below shows a recursive way to check whether two lists contain the same items but in different order.

The count() function returns a count of the number of items in a list that match the given item, and returns 0 otherwise. Ex: For list_num = [1, 3, 3, 4], list_num.count(3) returns 2.

Checkpoint

Checking list permutations

Checking list permutations; ch 12, video 6

Concepts in Practice

List permutations

Refer to the above animation. What would permu_check() return for each pair of lists below?

4.
list_num = [1, 7, 99, 2]
other_list = [99, 7, 1, 2]
  1. True
  2. False
5.
list_num = [22, 9, 15, 17, 15]
other_list = [17, 9, 22, 22, 15]
  1. True
  2. False
6.
honors_list = ["Bo", "Joe", "Sandy"]
first_names_of_team_1 = ["Sandy", "Bo", "Joe"]
  1. True
  2. False
7.
this_list = [1, 2, [3], [4], 5]
that_list = [[3], 1, 5, [4], 2]
  1. True
  2. False
8.
list_1 = [1, 2, 3]
list_2 = [1, 2, 3]
  1. True
  2. False

Try It

Remove duplicates

Write a recursive rem_dup() function that removes duplicates from a list.

Ex: List [5, 5, 2, 1, 3, 1, 6] should result in an output list [5, 2, 1, 3, 6].

Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.