How to join two lists in Python

Home /

Table of Contents

Brief about list

A list in Python is an ordered collection of items, which can be of different types (e.g. integer, string, float, etc.). Lists are created by placing items inside square brackets ‘[]‘ and separated by commas ‘,‘. Lists are mutable, meaning their contents can be changed after creation.

For example:

Python
# Creating a list of integers
list1 = [1, 2, 3, 4, 5]

# Creating a list of strings
list2 = ['apple', 'banana', 'cherry']

# Creating a list of mixed types
list3 = [1, 'dog', 3.14, True]

Join Two lists together

You can use the ‘extend()‘ method or the ‘+‘ operator to join two lists together in Python.

For example:

Python
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Using extend() method
list1.extend(list2)
print(list1) # Output: [1, 2, 3, 4, 5, 6]

# Using + operator
list1 = [1, 2, 3] + [4, 5, 6]
print(list1) # Output: [1, 2, 3, 4, 5, 6]

Create Array using List

In Python, there are two main ways to represent arrays: using lists or using the NumPy library. Here’s how to use a list as an array:

  1. Creating a list as an array:
Python
# create a list as an array
my_array = [1, 2, 3, 4, 5]

# print the array
print(my_array)

Output:

[1, 2, 3, 4, 5]

In this example, a list is created with the values 1, 2, 3, 4, and 5. This list can be treated as an array.

  1. Accessing elements in a list as an array:
Python
# create a list as an array
my_array = [1, 2, 3, 4, 5]

# access an element in the array
element = my_array[2]

# print the element
print(element)

Output:

3

In this example, the third element (index 2) in the array is accessed using square brackets.

  1. Modifying elements in a list as an array:
Python
# create a list as an array
my_array = [1, 2, 3, 4, 5]

# modify an element in the array
my_array[2] = 10

# print the updated array
print(my_array)

Output:

[1, 2, 10, 4, 5]

In this example, the third element (index 2) in the array is modified to the value 10 using square brackets.

Note that in Python, lists can hold elements of different types, so it is not necessary to specify a data type for the array. However, this can also make the array less efficient than using a NumPy array, which requires all elements to be of the same data type. If you need to perform complex mathematical operations on arrays, consider using the NumPy library instead.

Get Sublist from a List

In Python, you can use slicing to get a sublist from a list. Slicing allows you to specify a range of indices to extract from the list. Here are some examples:

  1. Get a sublist from the beginning of the list:
Python
# create a list
my_list = [1, 2, 3, 4, 5]

# get the first three elements of the list
sublist = my_list[:3]

# print the sublist
print(sublist)

Output:

[1, 2, 3]

In this example, slicing is used to get the first three elements (indices 0, 1, and 2) of the list.

  1. Get a sublist from the end of the list:
Python
# create a list
my_list = [1, 2, 3, 4, 5]

# get the last two elements of the list
sublist = my_list[-2:]

# print the sublist
print(sublist)

Output:

[4, 5]

In this example, slicing is used to get the last two elements (indices -2 and -1) of the list.

  1. Get a sublist from the middle of the list:
Python
# create a list
my_list = [1, 2, 3, 4, 5]

# get the middle three elements of the list
sublist = my_list[1:4]

# print the sublist
print(sublist)

Output:

[2, 3, 4]

In this example, slicing is used to get the three elements (indices 1, 2, and 3) in the middle of the list.

Note that when using slicing, the start index is inclusive and the end index is exclusive. So, in the example above, the slice ‘[1:4]‘ includes elements at indices 1, 2, and 3, but not the element at index 4.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article