How to create and use lists in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
n Python, a list is a data structure used to store multiple values in a single variable. Lists are enclosed in square brackets [] and the values inside are separated by commas.
Here is an example of how to create a list in Python:
#creating a list of numbers
numbers = [1, 2, 3, 4, 5]
#creating a list of strings
fruits = ['apple', 'banana', 'orange']
#creating a list of mixed data types
mixed_list = [1, 'hello', 3.14, True]
You can also create a list using the list() constructor which takes an iterable object as an argument:
#creating a list from a range of numbers
numbers = list(range(1, 6))
#creating a list from a string
letters = list("hello")
You can add, remove, or modify elements in a list, and use various list methods such as append(), insert(), remove() and more.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚