Compress lists of different sizes in Python

王林
Release: 2023-09-04 21:25:05
forward
1153 people have browsed it

Compress lists of different sizes in Python

Introduction

In Python, lists are one of the widely used methods of storing numeric or string values. They are mutable and defined by using square brackets []. Lists of this type can contain different elements, which can have different data types. Sometimes we may need to compress different lists in Python for data preprocessing purposes.

In this article, we will discuss the compression operation of lists and how to compress lists of different sizes in Python using different methods and techniques. This article will help one understand the compression operation of a list and help one perform the same if necessary.

Now let's start discussing lists and their compression operations.

List compression

As we all know, lists are a common way to store elements, which can contain numeric or string values. They are mutable types that are typically used when working with data sets when using Python.

The compression operation of lists means that we are actually compressing two different lists, or more simply, we are pairing the values of two different lists.

To clarify the idea behind it, let's take an example. Suppose we have two lists:

L1 = [1,2,3]

L2 = [‘一’, ‘二’, ‘三’]

As we can see above, we have two different lists, once we perform a compression operation on them, the output will be:

Zipped_List = [(1, ‘一’), (2, ‘二’), (3, ‘三’)]

Now let us discuss the use cases of compressed lists in Python.

Application of compressed list

Compressing two different lists of the same size or different sizes may help in many situations. Let’s discuss:

Dictionary representation:Compression operation on two different lists can help us create or represent the list as a dictionary. We can do the same thing by getting a list containing the keys and another list containing the dictionary values.

Data processing:In some cases, data processing is necessary in order to continue performing the task, and a common list may be needed instead of so many different lists. In this case, compression operations can be very helpful.

Data iteration:Compression operations can also be used when you want to iterate over list elements and want to perform some operation on them.

Compressed list

There are many ways to compress different lists, let's discuss some of them.

Method 1: Using For Loops and Enumerations

Using a for loop with an enumeration is one of the easiest ways to compress two lists of different sizes.

# Using For Loop with Enumerate #1. Define two lists 2. Run a for loop with enumerate 3. Zip the lists # define the two lists list1 = [1,2,3,4,5,6] list2 = [1, 5, 6] # print the original lists print ("The input list 1 is : " + str(list1)) print ("The input list 2 is : " + str(list2)) # for i, j run a for loop with enumerate # append the values with j res = [] for i, j in enumerate(list1): res.append((j, list2[i % len(list2)])) # print the zipped list print ("The Zip List from List 1 and 2 is : " + str(res))
Copy after login

As we can see in the above code, we are inputting two different lists, List 1 and List 2, which are of different sizes.

First we print the original list and then use the enumeration function to run a for loop that will append the list elements and compress both lists.

Output

The output of the following code is:

The input list 1 is : [1, 2, 3, 4, 5, 6] The input list 2 is : [1, 5, 6] The Zip List from List 1 and 2 is : [(1, 1), (2, 5), (3, 6), (4, 1), (5, 5), (6, 6)]
Copy after login

Method 2: Use Zip() method

Using the Zip() keyword can also help us compress two lists of different sizes. Here we can use specific keywords in the loop.

# using Zip() # define the list num_list = [1, 2, 3, 4] # numerical list str_list = ['one', 'two', 'three', 'four', 'none', 'none'] #string list # zip the lists with using zip() zipped_list = [(num, s) for num, s in zip(num_list, str_list)] print(zipped_list)
Copy after login

As we can see in the above code, we have two different lists of different sizes and we use zip() to append the list elements and compress the list.

Output

The output of the following code is:

[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
Copy after login

Method 3: Use Itertools

This is one of the classic ways to compress two lists of different sizes. Here we will use Itertools to compress the list.

# using the itertools # itertools + cycle # import the cycle from itertools from itertools import cycle # define two different lists list1 = [1,2,3,4,5,6,7] list2 = [10, 50, 21] # print the list1 and list2 print ("The input list 1 is : " + str(list1)) print ("The input list 2 is : " + str(list2)) # now use the cycle imported from itertools res = list(zip(list1, cycle(list2)) if len(list1) > len(list2) #check for conditions else zip(cycle(list1), list2)) # printing the zipped list print ("The Zip List from List 1 and 2 is: " + str(res))
Copy after login

As we can see in the code above, the itertools library is installed and loops are imported from it.

Then we defined two lists of different sizes and printed the same list. Next, the loop is used to compress the lists by passing both lists into the same list.

Output

The output of this code is:

The input list 1 is : [1, 2, 3, 4, 5, 6, 7] The input list 2 is : [10, 50, 21] The Zip List from List 1 and 2 is : [(1, 10), (2, 50), (3, 21), (4, 10), (5, 50), (6, 21), (7, 10)]
Copy after login

in conclusion

In this article, we discussed about lists, what are the compression operations for lists, what are the applications for the same and how to compress two lists of different sizes in Python.

We have discussed a total of 3 methods using which you can compress a list in Python and anyone can compress a list as per the problem statement and requirements. This article will help one understand the compression operation of a list and help one perform the same when needed.

The above is the detailed content of Compress lists of different sizes in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!