List
In Python, a list is a mutable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Lists are defined by enclosing the items (elements) in square brackets [].
Creating a List
my_list = [1, 2, 3, 'Python', 5.0]
In this example, my_list is a list containing three integers, a string, and a float.
Accessing Elements
You can access an item in a list by referring to its index number.
print(my_list[3]) # Outputs: Python
Modifying Elements
You can change the value of a specific item by referring to its index number.
my_list[1] = 'two'
print(my_list) # Outputs: [1, 'two', 3, 'Python', 5.0]
Adding Elements
You can add items to the end of the list using the append() method.
my_list.append('new item')
Adding Elements
You can add items to the end of the list using the append() method.
my_list.append('new item')
To insert an item at a specified index, use the insert() method.
my_list.insert(1, 'inserted item')
Removing Elements
Use the remove() method to remove a specific item.
my_list.remove('Python')
Use the pop() method to remove an item at a specific index, or the last item if the index is not specified.
my_list.pop(1) # Removes 'inserted item'
List Slicing
List slicing allows you to create a new list from an existing one.
new_list = my_list[1:4] # Creates a new list with items from index 1 to index 3 from my_list
Iterating over a List
You can loop through the list items by using a for loop.
for item in my_list:
print(item)
List Comprehension
List comprehension provides a concise way to create lists.
squares = [x * x for x in range(10)] # Generates a list of squares from 0 to 81
List Length
To determine how many items a list has, use the len() function.
print(len(my_list))
Nested Lists
A list can contain other lists.
nested_list = [[1, 2, 3], ['a', 'b', 'c']]
Sorting Lists
You can sort a list using the sort() method or the sorted() function.
numbers = [3, 1, 4, 1, 5, 9, 2]
numbers.sort() # Sorts the list in-place
sorted_numbers = sorted(numbers) # Returns a new sorted list, original list remains unchanged
Clearing a List
You can remove all items in a list using the clear() method.
my_list.clear()
Concatenating Lists
You can combine two or more lists using the + operator.
combined_list = my_list + another_list
Multiplying Lists
You can create a new list by repeating an existing list a specified number of times using the * operator.
repeated_list = my_list * 3
Checking Membership
To check if a certain item is present in a list, you can use the in keyword.
if 'Python' in my_list:
print('Python is in the list')
List to String
You can convert a list of strings into a single string using the join() method.
str_list = ['Python', 'is', 'awesome']
joined_str = ' '.join(str_list) # Joins the list into a string with space as a separator
print(joined_str) # Outputs: Python is awesome
Summary
Lists in Python are one of the most versatile and commonly used data structures, handy for storing and manipulating ordered collections of items, which can be of any type, and are allowed to be heterogeneous.