What are the differences between list, sequence and slice data types in Python?


We will learn about the differences between list, sequence, and slice data types in Python in this article.

Lists − In Python, a list is a type of data that can hold multiple values in a single variable. You can think of it like a shopping list that contains multiple items. For example, you could have a list of numbers like [1, 2, 3, 4, 5] or a list of names like ["John", "Jane", "Bob", "Sue"].

Number_List = [1, 2, 3, 4, 5]

Name_List = ["John", "Jane", "Bob", "Sue"]

Sequences − In Python, a sequence is a type of data that represents a sequence of values. This could be a string of characters, like "hello" or a range of numbers, like 1 to 10. You can think of a sequence like a line of people waiting to get into a concert. Each person has a specific place in the line, just like each value in a sequence has a specific position.

Slices − A slice is a way to extract a part of a list or sequence. You can think of it like cutting a slice of cake out of a larger cake. For example, if you have a list of numbers [1, 2, 3, 4, 5], you could use a slice to extract only the first three values [1, 2, 3] or the last two values [4, 5]. Slices can also be used on sequences like strings to extract specific parts of the string.

Examples that demonstrate the differences between list, sequence, and slice data types in Python −

Lists

Example

In this example, the code creates a list called my_list that contains three string values: "apple", "banana", and "cherry".

my_list = ["apple", "banana", "cherry"]

Sequences

Example

In this example, the code creates a sequence called my_sequence that represents the range of numbers from 1 to 5.

my_sequence = range(1, 6)
for i in range(1,6):
   print(i)

Output

1
2
3
4
5

Slices

Example

This code creates a list called my_list that contains five string values. It then creates a slice of my_list called sliced_list that contains only the values at positions 1, 2, and 3 (i.e., "banana", "cherry", and "date").

my_list = ["apple", "banana", "cherry", "date", "elderberry"]
sliced_list = my_list[1:4]
print(sliced_list)

Output

['banana', 'cherry', 'date']

Example

This code creates a string called my_string that contains the text "hello world". It then creates a slice of my_string called sliced_string that contains only the first five characters (i.e., "hello").

my_string = "hello world"
sliced_string = my_string[0:5]
print(sliced_string)

Output

hello

The following examples highlight the differences between list, sequence, and slice data types in Python −

Example of modifying a list

This code creates a list called my_list that contains three integer values. It then modifies the value at position 1 (i.e., the second value in the list) to be 5. This illustrates that lists are mutable, meaning their values can be changed after they are created.

my_list = [1, 2, 3]
my_list[1] = 5

Example of iterating over a sequence

This code creates a sequence called my_sequence that represents the range of numbers from 1 to 5. It then iterates over each value in the sequence using a for loop and prints it to the console. This illustrates that sequences are iterable, meaning their values can be accessed one at a time.

my_sequence = range(1, 6)
for value in my_sequence:
   print(value)

Output

1
2
3
4
5

Example of concatenating lists

This code creates two lists called list1 and list2 that contain three integer values each. It then concatenates the two lists using the + operator and stores the result in a new list called concatenated_list. This illustrates that lists can be combined using the + operator.

list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list)

Output

[1, 2, 3, 4, 5, 6]

Example of slicing a string

This code creates a string called my_string that contains the text "hello world". It then creates a slice of my_string called sliced_string that contains only the characters starting from position 6 (i.e., "world"). This illustrates that slices can be used on strings as well as lists and sequences.

my_string = "hello world"
sliced_string = my_string[6:]
print(sliced_string)

Output

world

In summary, these code examples demonstrate that lists are a data type that can hold multiple values, sequences are another data type that represent a sequence of values, and slices are yet another data type which allow us to extract a part of a list or sequence.

Updated on: 08-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements