Open In App

Python Memory Consumption: Strings vs Lists

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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")


Output

Size 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")


Output

Size 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")


Output

Size 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")


Output

Size 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")


Output

Size 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")


Output

Size 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")


Output

Size 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")


Output

Size 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")


Output

Size of Modified String: 62 bytes
Size of Modified List: 232 bytes


Memory Consumption Comparison Table of Strings and Lists

Aspect

Strings

Lists

Creation

Efficient

More memory overhead, especially for large sequences

Concatenation

Efficient

More efficient using join operation

Memory Growth with Size

Predictable

Grows with the size of the list

Modification (In-Place)

Immutability, creates new string

Mutable, can modify in-place, less memory overhead



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads