Start / Blog / Python / Create Python list(s) with explanation and examples

Create Python list(s) with explanation and examples

Summarize with ChatGPT

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!

  1. 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
  1. 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]
  1. 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]
  1. Join lists - To connect two or more lists, we can use the + Operator or the extend() Use method:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined_list = list1 + list2
print(joined_list)  # Ausgabe: [1, 2, 3, 4, 5, 6]
  1. 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]
  1. Create an empty list - To create an empty list, simply use two square brackets []:
empty_list = []
  1. 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)
  1. 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
  1. 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]
  1. 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}]
  1. 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
  1. Concatenate lists - To concatenate lists, use the + Operator or the extend() Method:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list)  # Ausgabe: [1, 2, 3, 4, 5, 6]
  1. 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]
  1. 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]
  1. Check whether a list contains a value - To check whether a list contains a value, use the in Keyword:
my_list = [1, 2, 3, 4, 5]
print(3 in my_list)  # Ausgabe: True
  1. Shuffle a list (shuffle) - To arrange the elements of a list randomly, use the shuffle() Function from the random Module:
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]
  1. 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")
  1. 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.0

Conclusion

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

Did you find this page helpful?

Thank you for your feedback!

Would you give me feedback? (anonymous)

We develop AI software for companies and deliberately avoid annoying advertising banners. Through our articles, we document topics that occupy and interest us and also finance our daily bread.

As our content is free of charge, your feedback is our praise.

Each author reads your anonymous feedback personally, although AI could automate it, and integrates constructive suggestions directly into the next revision or uses it as inspiration for the next article.



    </article
    en_USEN