DEV Community

Cover image for Python Lists
Peter
Peter

Posted on

2

Python Lists

In python, a list is a collection of items that can be of any data type; i.e. strings, numbers, etc. Lists are ordered and mutable, meaning that you can change the elements in a list after creating the list.

Creating a list
To create a list in Python, you can enclose a comma-separated sequence of values in square brackets []. For example:

numbers = [1, 2, 3, 4, 5]
names = ['Alice', 'Bob', 'Charlie']

Enter fullscreen mode Exit fullscreen mode

Accessing elements
To access an element in a list, you can use its index in square brackets []. The first element in the list has an index of 0.

numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # Output: 1
print(numbers[2]) # Output: 3
Enter fullscreen mode Exit fullscreen mode

Modifying elements
To modify an element in a list, you can simply assign a new value to it using its index.

numbers = [1, 2, 3, 4, 5]
numbers[2] = 42
print(numbers) # Output: [1, 2, 42, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Adding elements
To add an element to a list, you can use the append method. This method adds an element to the end of the list.

numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers) # Output: [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

Removing elements
To remove an element from a list, you can use the remove method. This method removes the first occurrence of the given value from the list.

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

Enter fullscreen mode Exit fullscreen mode

Sorting Python Lists

To sort a list in python, you can use the built-in 'sorted' function. By default, this function will sort the list in ascending order, but you can reverse the order by passing the argument 'reverse=True'.

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
# Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)
# Output: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]

Enter fullscreen mode Exit fullscreen mode

If you want to sort a list in-place, you can use the 'list.sort' method. This method sorts the list in-place and does not return a new list.

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort()
print(numbers)
# Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

numbers.sort(reverse=True)
print(numbers)
# Output: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more