Python Memory Consumption: Strings vs Lists
Last Updated :
30 Jan, 2024
Programming memory use is an important consideration, particularly when working with big datasets or resource-intensive programs. Writing effective Python code requires knowing how various data structures affect memory utilization. This article examines how lists and strings use memory differently in Python and explains the basic ideas behind each.
Memory Consumption of Strings and Lists in Python
Below are some examples by which we can understand about the memory consumption in strings and lists in Python:
Python Memory Size of Strings
Python strings are immutable, which means that once they are created, their contents cannot be altered. A string's memory use is determined by both the character sizes and extra overhead. To get the size of an object in bytes, use the sys.getsizeof() function.
Python3
import sys
string_example = "Hello, World!"
print(f"Size of string: {sys.getsizeof(string_example)} bytes")
OutputSize of string: 62 bytes
Memory of Python List
Python lists are dynamic arrays that may expand or contract as required. A list's memory needs are determined by several variables, including the quantity and kind of its items as well as the system architecture. More RAM is often needed for larger lists.
Python3
import sys
list_example = [1, 2, 3, 4, 5]
print(f"Size of the list: {sys.getsizeof(list_example)} bytes")
OutputSize of the list: 112 bytes
How Much Memory Will a List with One Million Elements Take Up in Python?
The following example may be used to determine how much memory a list with one million entries would require. In this example, we have created a list consisting one million elements and we are finding the memory size of the list.
Python3
import sys
large_list = list(range(1000000))
print(f"Size of the list with one million elements: {sys.getsizeof(large_list)} bytes")
OutputSize of the list with one million elements: 9000120 bytes
Memory Size of Strings having one million size in Python
As was previously discussed, variables like length and encoding affect how big strings are stored in memory. You may use sys.getsizeof() to measure it.
Python3
import sys
string_example = "Hello, World! "*(1000000)
print(string_example)
print(f"Size of the string: {sys.getsizeof(string_example)} bytes")
OutputSize of the string: 62 bytes
How Much Memory Does a Python Set Take Up?
In Python, sets contain an underlying data structure that causes a memory cost. Sys.getsizeof() may be used to calculate the size.
Python3
import sys
set_example = {1, 2, 3, 4, 5}
print(f"Size of the set: {sys.getsizeof(set_example)} bytes")
OutputSize of the set: 744 bytes
How Do I Limit Python Memory Usage?
You may use third-party libraries like resource_limits or tools like resource module to restrict how much memory Python uses. Using system-level tools and configuring environment variables like PYTHONMALLOC may also affect memory use.
Memory Consumption of Simple String vs List
In this example, a list of the characters in the string and a basic string are formed. The sys.getsizeof() method is used to compare the sizes of the list and the string. In Python, strings store characters more efficiently in memory than lists do.
Python3
import sys
# Example 1
string_example = "Hello, World!"
list_example = list(string_example)
string_size = sys.getsizeof(string_example)
list_size = sys.getsizeof(list_example)
print(f"Size of String: {string_size} bytes")
print(f"Size of List: {list_size} bytes")
OutputSize of String: 62 bytes
Size of List: 232 bytes
Memory Consumption of Large String vs List
In this example, a lengthy string and a list of the string's characters are produced. To show how the memory usage increases with string size, the sizes are compared. When it comes to character memory use, strings often behave more predictably than lists.
Python3
import sys
# Example 2
large_string = "A" * 2000000
list_example = list(large_string)
string_size = sys.getsizeof(large_string)
list_size = sys.getsizeof(list_example)
print(f"Size of Large String: {string_size} bytes")
print(f"Size of List: {list_size} bytes")
OutputSize of Large String: 2000049 bytes
Size of List: 18000120 bytes
Memory Consumption of Concatenation of Strings vs Lists in Python
The memory use of a list of distinct strings and a concatenated string using "".join() are compared in this example. When working with a large number of strings, the join operation uses less memory than building a list.
Python3
import sys
# Example 3
strings = ["Hello", "World", "!"]
joined_string = "".join(strings)
list_example = list(strings)
joined_string_size = sys.getsizeof(joined_string)
list_size = sys.getsizeof(list_example)
print(f"Size of Joined String: {joined_string_size} bytes")
print(f"Size of List: {list_size} bytes")
OutputSize of Joined String: 60 bytes
Size of List: 120 bytes
Python Memory Consumption of Modifying String vs List
We change the first character in the list and the string in this example. This illustrates that altering a string by starting from scratch uses more memory than altering a list in-place, which uses less memory.
Python3
import sys
# Example 4
string_example = "Hello, World!"
list_example = list(string_example)
# Modifying the first character
string_example_modified = "J" + string_example[1:]
list_example[0] = "J"
string_modified_size = sys.getsizeof(string_example_modified)
list_modified_size = sys.getsizeof(list_example)
print(f"Size of Modified String: {string_modified_size} bytes")
print(f"Size of Modified List: {list_modified_size} bytes")
OutputSize of Modified String: 62 bytes
Size of Modified List: 232 bytes
Memory Consumption Comparison Table of Strings and Lists
|
Efficient
| More memory overhead, especially for large sequences
|
Efficient
| More efficient using join operation
|
Predictable
| Grows with the size of the list
|
Immutability, creates new string
| Mutable, can modify in-place, less memory overhead
|
Similar Reads
Python | Ways to merge strings into list
Given n strings, the task is to merge all strings into a single list. While developing an application, there come many scenarios when we need to operate on the string and convert it as some mutable data structure, say list. There are multiple ways we can convert strings into list based on the requir
4 min read
List of strings in Python
A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read
Python - List of float to string conversion
When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read
Python - Merge consecutive empty Strings
Sometimes, while working with python lists, we can have a problem in which we need to perform a merge operation on a string of lists. This merge is consecutive to convert multiple spaces to one. Let's discuss a certain way in which this can be performed. Method #1 : Using loop, This is a brute way i
6 min read
Python | Count String occurrences in mixed list
Sometimes, while working with data, we can have a problem in which we need to check for the occurrences of a particular data type. In this, we can also have a problem in which we need to check for string occurrences. Let's discuss certain ways in which this task can be performed. Method #1 : Using i
8 min read
Create a List of Strings in Python
Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Python | Convert List of lists to list of Strings
Interconversion of data is very popular nowadays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + joi
4 min read
Convert list of strings to list of tuples in Python
Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
Python | Convert String to list of tuples
Sometimes, while working with data, we can have a problem in which we have a string list of data and we need to convert the same to list of records. This kind of problem can come when we deal with a lot of string data. Let's discuss certain ways in which this task can be performed. Method #1: Using
8 min read
Python | Find Mixed Combinations of string and list
Sometimes, while working with Python, we can have a problem in which we need to make combinations of string and character list. This type of problem can come in domains in which we need to interleave the data. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + e
5 min read