In this blog post, we will take an in-depth look at Python list(s), one of the most basic and commonly used data structures in Python. We will cover various aspects of lists and show you how to use them effectively.
We'll cover topics such as determining the length of a list, removing duplicates, joining lists and much more. Let's dive in!
- Determine the length of a list - To determine the length of a list, we use the
len()Function. Here is a short example:
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # Ausgabe: 5- Remove duplicates from a list - To remove duplicates from a list, we can use a set to create a new list without duplicates:
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(my_list))
print(unique_list) # Ausgabe: [1, 2, 3, 4, 5]- Flattening a list - To convert a nested list to a flat list, we can use a list comprehension:
nested_list = [[1, 2], [3, 4], [5, 6]]
flat_list = [item for sublist in nested_list for item in sublist]
print(flat_list) # Ausgabe: [1, 2, 3, 4, 5, 6]- Join lists - To connect two or more lists, we can use the
+Operator or theextend()Use method:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined_list = list1 + list2
print(joined_list) # Ausgabe: [1, 2, 3, 4, 5, 6]- List Slicing - List slicing allows us to select parts of a list based on indices:
my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[1:4] # Ausgabe: [2, 3, 4]
- Create an empty list - To create an empty list, simply use two square brackets
[]:
empty_list = []
- Determine the size of a list in bytes - To determine the size of a list in bytes, use the
sys.getsizeof()Function:
import sys
my_list = [1, 2, 3, 4, 5]
print(sys.getsizeof(my_list)) # Ausgabe: 104 (abhängig von der Python-Version und dem System)- Calculate the sum of a list - To calculate the sum of a list of numbers, use the
sum()Function:
my_list = [1, 2, 3, 4, 5]
print(sum(my_list)) # Ausgabe: 15- Filter a list - To filter a list based on a condition, use the
filter()Function or a List Comprehension:
my_list = [1, 2, 3, 4, 5]
filtered_list = list(filter(lambda x: x % 2 == 0, my_list)) # Ausgabe: [2, 4]
# Alternativ mit List Comprehension
filtered_list = [x for x in my_list if x % 2 == 0] # Ausgabe: [2, 4]- Lists of lists and dictionaries - In Python, you can create lists of lists or dictionaries to display complex data structures:
list_of_lists = [[1, 2], [3, 4], [5, 6]]
list_of_dicts = [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]
- Find a value in a list - To find a value in a list, use the
index()Method:
my_list = [1, 2, 3, 4, 5]
index = my_list.index(3) # Ausgabe: 2- Concatenate lists - To concatenate lists, use the
+Operator or theextend()Method:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list) # Ausgabe: [1, 2, 3, 4, 5, 6]- Output a list - To output a list in Python, use the
print()Function:
my_list = [1, 2, 3, 4, 5]
print(my_list) # Ausgabe: [1, 2, 3, 4, 5]- Tuples vs. lists - Tuples and lists are both sequential data types, but tuples are immutable, while lists are mutable:
my_tuple = (1, 2, 3)
my_list = [1, 2, 3]- Check whether a list contains a value - To check whether a list contains a value, use the
inKeyword:
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Ausgabe: True- Shuffle a list (shuffle) - To arrange the elements of a list randomly, use the
shuffle()Function from therandomModule:
import random
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list) # Ausgabe: Zufällige Anordnung, z.B. [3, 5, 1, 4, 2]- Check whether a list is empty - To check whether a list is empty, use the
len()function or check the list directly:
empty_list = []
if not empty_list:
print("Die Liste ist leer")- Calculate the average of a list - To calculate the average of a list of numbers, use the
sum()function and divide by the length of the list:
my_list = [1, 2, 3, 4, 5]
average = sum(my_list) / len(my_list)
print(average) # Ausgabe: 3.0Conclusion
In this guide, we have covered various aspects of Python list(s) such as determining the length, removing duplicates, joining lists and much more. Python lists are a basic and versatile tool that will help you write efficient and clean code. Now that you are familiar with the various functions and methods, you can use Python lists effectively in your projects.
You can find more Python examples in the articles
- Python Twister: 8 absurd examples of complex code
- Data pipelines with Python "how to" - A comprehensive guide
