Lists
Lists are a type of data structure in Python that can hold an ordered collection of items. They are mutable, which means that you can modify their contents by adding, removing, or changing elements. Lists can hold different data types, including strings, integers, and even other lists. They are created using square brackets, and items are separated by commas.
Creating a list:
Lists are a type of container that can hold multiple values. They are created by enclosing the values in square brackets and separating them by commas. Here’s an example of how to create a list of integers:
numbers = [1, 2, 3, 4, 5]
You can also create a list of strings:
fruits = ["apple", "banana", "cherry", "orange"]
And you can even create a list of different types of values:
mixed_list = [1, "apple", True, 3.14]
Indexing:
You can access individual elements of a list by their index. The index starts at 0 for the first element and increases by 1 for each subsequent element. You can use square brackets and the index number to access an element of a list. Here’s an example:
fruits = ["apple", "banana", "cherry", "orange"]
print(fruits[1]) # Output: "banana"
You can also use negative indexing to access elements from the end of the list. The last element has an index of -1, the second to last has an index of -2, and so on. Here’s an example:
fruits = ["apple", "banana", "cherry", "orange"]
print(fruits[-1]) # Output: "orange"
Slicing:
You can also access a range of elements in a list by slicing it. Slicing is done using a colon to separate the starting and ending indices. Here’s an example:
fruits = ["apple", "banana", "cherry", "orange"]
print(fruits[1:3]) # Output: ["banana", "cherry"]
This will return a new list containing the elements from index 1 (inclusive) up to index 3 (exclusive).
You can also omit the starting or ending index to slice from the beginning or end of the list. Here’s an example:
fruits = ["apple", "banana", "cherry", "orange"]
print(fruits[:2]) # Output: ["apple", "banana"]
print(fruits[2:]) # Output: ["cherry", "orange"]
Modifying a list:
Lists are mutable, which means you can change their contents. You can assign a new value to an individual element by using its index. Here’s an example:
fruits = ["apple", "banana", "cherry", "orange"]
fruits[1] = "kiwi"
print(fruits) # Output: ["apple", "kiwi", "cherry", "orange"]
You can also use slicing to modify a range of elements. Here’s an example:
fruits = ["apple", "banana", "cherry", "orange"]
fruits[1:3] = ["kiwi", "melon"]
print(fruits) # Output: ["apple", "kiwi", "melon", "orange"]
This will replace the elements at indices 1 and 2 with “kiwi” and “melon”, respectively.
You can also concatenate two lists using the + operator. Here’s an example:
fruits = ["apple", "banana", "cherry"]
more_fruits = ["orange", "mango"]
all_fruits = fruits + more_fruits
print(all_fruits) # Output: ["apple", "banana", "cherry", "orange", "mango"]
The order in which you concatenate the lists matters. If you want to add the elements of one list to the end of another list, use the extend() method instead. Here’s an example:
fruits = ["apple", "banana", "cherry"]
more_fruits = ["orange", "mango"]
fruits. extend(more_fruits)
print(fruits) # Output: ["apple", "banana", "cherry", "orange", "mango"]
You can also remove elements from a list using the remove() method. Here’s an example:
fruits = ["apple", "banana", "cherry"]
fruits. remove("banana")
print(fruits) # Output: ["apple", "cherry"]
You can also use the pop() method to remove an element from a specific index. This method returns the removed element. Here’s an example:
fruits = ["apple", "banana", "cherry"]
removed_fruit = fruits. pop(1)
print(removed_fruit) # Output: "banana"
print(fruits) # Output: ["apple", "cherry"]
Finally, you can use the len() function to get the length of a list (i.e., the number of elements it contains). Here’s an example:
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # Output: 3
for the Next step Click here