🐍
Python Extra
  • Python: An Introduction
  • Python Getting Started
  • Python Syntax and Execution
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
    • Python - Slicing Strings
    • Python - Modify Strings
    • Python - String Concatenation
    • Python - Format Strings
    • Python - Escape Characters
    • Python - String Methods
  • Python Booleans
  • Python Operators
  • Python Lists
Powered by GitBook
On this page
  • Creating Lists
  • List Properties
  • List Items and Indexing
  • List Length
  • List Items - Data Types
  • Data Type of a List
  • Using the list() Constructor
  • Collections in Python

Python Lists

Lists are a versatile and fundamental data structure in Python used to store multiple items in a single variable. They are one of the four built-in data types in Python used to store collections of data, with the others being tuples, sets, and dictionaries. Lists are created using square brackets [].

Creating Lists

You can create a list by placing a comma-separated sequence of items inside square brackets. Here's an example:

mylist = ["apple", "banana", "cherry"]

List Properties

Lists in Python have several important properties:

  1. Ordered: List items are ordered, meaning they have a specific sequence or arrangement. This order will not change unless you modify the list.

  2. Changeable: Lists are mutable, which means you can change, add, or remove items after the list is created.

  3. Allow Duplicates: Lists can contain duplicate values. This is because each item in a list is identified by its index position, and having duplicates at different positions is allowed.

List Items and Indexing

List items are indexed, starting with index 0 for the first item, index 1 for the second item, and so on. You can access list items by referring to their index number.

Example:

mylist = ["apple", "banana", "cherry"]
print(mylist[0])  # Access the first item (index 0)

List Length

You can use the len() function to determine the number of items in a list. It returns the length of the list.

Example:

mylist = ["apple", "banana", "cherry"]
print(len(mylist))  # Outputs 3

List Items - Data Types

Lists can contain items of various data types. A single list can include elements such as strings, integers, and even boolean values. There is no restriction on mixing different data types in a list.

Example:

list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]

Data Type of a List

From Python's perspective, a list is an object with the data type 'list'. You can use the type() function to check the data type of a list.

Example:

mylist = ["apple", "banana", "cherry"]
print(type(mylist))  # Outputs <class 'list'>

Using the list() Constructor

You can also create a list using the list() constructor. This constructor can take an iterable (e.g., a tuple or string) as an argument and convert it to a list.

Example:

thislist = list(("apple", "banana", "cherry"))
print(thislist)

Collections in Python

Python offers various collection data types. Lists are just one of these data types. Other collection types include:

  • Tuple: A collection that is ordered and unchangeable. Allows duplicate members.

  • Set: A collection that is unordered, unchangeable, and unindexed. Does not allow duplicate members.

  • Dictionary: A collection that is ordered (Python 3.7+) and changeable. Stores data as key-value pairs.

Understanding the properties and use cases of these collection data types is essential for efficient and meaningful programming in Python.

Lists are commonly used in Python for tasks like storing data, iterating through elements, and performing various operations on collections. They provide flexibility and ease of use, making them a fundamental part of Python programming.

PreviousPython Operators

Last updated 1 year ago