Python Lists: A Beginner’s Guide to Storing Data in Python Lists.

Nipun Madusanka
2 min readMar 15, 2023

--

Photo by Maxwell Nelson on Unsplash

Python is a popular programming language used in various fields such as web development, data science, and machine learning. One of the basic data storage methods in Python is “Lists”. Lists are a versatile data structure that can be used to store multiple items in a single variable. They are created using square brackets.

To create a list in Python, you simply need to assign a list of values to a variable using the following syntax:

myList = [1,2,3,4,5,6,7]
print(myList)

It’s possible to store different types of data in one list. For instance, you can have a list with both integers and strings:

myList2 = [2,3,"number","x"]
print("muList2 has: ",myList2)

The len() function is used to see how many items are in a list:

myList2 = [2,3,"number","x"]
print(len(myList2))

Index numbers are used to access each value in the list:

myList2 = list((2,3,"number","x"))
print(myList2[2])

To access values in a certain range, you need to enter the start and end values for the range. This is achieved as follows:

myList = [1,2,3,4,5,6,7,8,9]
print(myList[2:6])

To change a specific value in the list, you need to refer to the relevant index number:

myList2 = list((2,3,"number","x"))
myList2[2] = "Changed"
print(myList2)

To add a new value anywhere in the list, use the insert() method:

myList2 = list((2,3,"number","x"))
myList2.insert(2,"insert")
print(myList2)

To add a value to the end of the list, use the append() method:

myList2 = list((2,3,"number","x"))
myList2.append("last_value")
print(myList2)

You can join two lists together using the extend() method:

list1 = ["apple", "banana", "cherry"]
list2 = ["mango", "pineapple", "papaya"]
list1.extend(list2)
print(list1)

To remove a value from the list, you can use either the remove() method or the pop() method:

list1 = ["apple", "banana", "cherry"]
list1.remove("apple")
print(list1)
list1 = ["apple", "banana", "cherry"]
list1.pop(2)
print(list1)

In summary, lists are a useful and versatile data storage method in Python. By learning how to create, access, and modify lists, you’ll be able to handle and manipulate data more effectively in your Python programs.

Buy Me a coffee: https://www.buymeacoffee.com/jmnipun

Feel free to get in touch with me. https://linktr.ee/jmnipun

--

--

Nipun Madusanka

Learning about new technologies is a passion of mine, and I find it enjoyable. My ultimate goal is to utilize technology in ways that benefit society.