Crus4

logo

Python Lists


Topics Covered

Python Lists are collection of elements, which can be of any type (e.g. integers, strings, floats, etc.). Lists are mutable, meaning that you can add, remove, and modify elements in the list. Lists are also very flexible, allowing you to perform a wide variety of operations on them.

Creating a List in Python

To create a lists in Python, you can use square brackets [ ] and separate the elements with commas. Here’s an example of a simple list:

my_list = [1, 2, 3, 4, 5]

This creates a list with five elements: 1, 2, 3, 4, and 5. You can also create a list with elements of different types. Like this:

my_list = [1, "Hello", 3.14, True]

This creates a list with four elements: an integer, a string, a float, and a Boolean value.

Accessing Elements in a List

We can access individual elements in a list using their index numbers. The index of the first element is 0, the second element is 1, and so on. To access an element, use square brackets and the index number. For Example:

my_list = [1, 2, 3, 4, 5]
print(my_list[0])  # Output: 1
print(my_list[2])  # Output: 3

We can also use negative indexing to access elements from the end of the list. Like this:

my_list = [1, 2, 3, 4, 5]
print(my_list[-1])  # Output: 5
print(my_list[-3])  # Output: 3

Slicing Lists

We can also access a range of elements in a list using slicing. To slice a list, use square brackets and specify the start and end indexes separated by a colon. Here is an example:

my_list = [1, 2, 3, 4, 5]
print(my_list[1:3])  # Output: [2, 3]

This returns a new list with elements from index 1 up to (but not including) index 3.

Modifying Elements in a List

Lists are mutable, so we can change the value of an element in a list. Here is an example.

my_list = [1, 2, 3, 4, 5]
my_list[0] = 6
print(my_list)  # Output: [6, 2, 3, 4, 5]

Adding Elements to a List

To add elements to a list we can use the append() method. Here is an example:

my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

Adding Multiple Elements to a List

We can add multiple elements to a list using the extend() method. Here’s an example:

my_list = [1, 2, 3, 4, 5]
my_list.extend([6, 7, 8])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8]

Python – List Length

To get the length of a list in Python, we can use the built-in len() function. Here is an example:

my_list = [1, 2, 3, 4, 5]
print(len(my_list)) #output: 5
my_list2 = ["hello", "world", "I", "am", "Learning", "Python"]
print(len(my_list2)) #output: 6

Python – List items Data Types

In Python, a list can contain elements of different Data Types. A list element can be an Integer, String, Boolean etc. Here’s an example:

Example

my_list = [1, "hello", 3.14, True]

In an above example, the my_list contains elements of different data types: an integer (1), a string ("hello"), a floating-point number (3.14), and a boolean value (True).

Example

my_list = [1, "hello", 3.14, True]

for item in my_list:
    print(type(item))
#output: <class 'int'>
, <class 'str'>
, <class 'float'>
, <class 'bool'>

In this example, the for loop iterates over each element in the my_list list and prints its Data Type using the type() function.

Python – sort() Method

In Python, we can use the built-in list data type to create an ordered list. By default, a list in Python maintains the order in which the elements are added.

Here is an example of creating an ordered list in Python using the sort() method.

Example

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]

# Sort the list in ascending order
my_list.sort()

# Print the sorted list
print(my_list)
#output: [1, 1, 2, 3, 3, 4, 5, 5, 6, 9]

In an above example, we first created a list called my_list with unsorted elements. We then used the sort() method to sort the list in ascending order. Finally, we printed the sorted list using the print() function.

Python – Reversing a List

To reverse a list in Python, we can use the built-in reverse() method of a list object. Alternatively, you can use slicing to create a new list with the elements in reverse order. Here are examples of both methods:

Using the reverse() method:

my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)
# output: [5, 4, 3, 2, 1]

Using Slicing

my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]
print(reversed_list) # output: [5, 4, 3, 2, 1]

Note that the reverse() method modifies the original list in place, while slicing creates a new reversed list.


Share This Post!

Python Lists (with examples)