Category: 5. Lists

  • Python List Comprehensions

    Summary: in this tutorial, you’ll learn about Python List comprehensions that allow you to create a new list from an existing one. Introduction to Python list comprehensions In programming, you often need to transform elements of a list and return a new list. For example, suppose that you have a list of five numbers like this: And…

  • How to Use the Python Reduce() function to Reduce a List into a Single Value

    Summary: in this tutorial, you’ll learn how to use the Python reduce() function to reduce a list into a single value. Reducing a list Sometimes, you want to reduce a list to a single value. For example, suppose that you have a list of numbers: And to calculate the sum of all elements in the scores list, you can use…

  • How to Filter List Elements in Python

    Summary: in this tutorial, you’ll learn how to filter list elements by using the built-in Python filter() function. Introduction to Python filter() function Sometimes, you need to iterate over elements of a list and select some of them based on specified criteria. Suppose that you have the following list of scores: To get all elements from the scores list where each element…

  • How to Find the Index of an Element in a List

    Summary: in this tutorial, you’ll learn how to find the index of an element in a list. To find the index of an element in a list, you use the index() function. The following example defines a list of cities and uses the index() method to get the index of the element whose value is ‘Mumbai’: It returns 3 as…

  • How to Use a For Loop to Iterate over a List

    Summary: in this tutorial, you’ll learn how to use the Python for loop to iterate over a list in Python. Using Python for loop to iterate over a list To iterate over a list, you use the for loop statement as follows: In this syntax, the for loop statement assigns an individual element of the list to the item variable in each iteration. Inside the body…

  • Python List Slice

    Summary: in this tutorial, you’ll learn about Python list slice and how to use it to manipulate lists effectively. Introduction to Python List slice notation Lists support the slice notation that allows you to get a sublist from a list: In this syntax, the begin, end, and step arguments must be valid indexes. And they’re all optional. The begin index defaults to…

  • Python sorted

    Summary: in this tutorial, you’ll learn how to use the Python sorted() function to sort a list. Introduction to the Python sorted() function The sort() method sorts a list in place. In other words, it changes the order of elements in the original list. To return the new sorted list from the original list, you use the sorted() function: The sorted() function doesn’t modify the…

  • Python Sort List

    Summary: in this tutorial, you’ll learn how to use the Python List sort() method to sort a list. Introduction to the Python List sort() method To sort a list, you use the sort() method: The sort() method sorts the original list in place. It means that the sort() method modifies the order of elements in the list. By default, the sort() method sorts the elements of a list…

  • Python Tuples

    Summary: in this tutorial, you’ll learn about Python tuples and how to use them effectively. Introduction to Python tuples Sometimes, you want to create a list of items that cannot be changed throughout the program. Tuples allow you to do that. A tuple is a list that cannot change. Python refers to a value that cannot change…

  • Python List

    Summary: in this tutorial, you’ll learn about Python List type and how to manipulate list elements effectively. What is a List A list is an ordered collection of items. Python uses the square brackets ([]) to indicate a list. The following shows an empty list: Typically, a list contains one or more items. To separate…