Split list into variables Python

Data can take many shapes and forms - and it's oftentimes represented as strings.

Be it from a CSV file or input text, we split strings oftentimes to obtain lists of features or elements.

In this guide, we'll take a look at how to split a string into a list in Python, with the split[] method.

Split String into List in Python

The split[] method of the string class is fairly straightforward. It splits the string, given a delimiter, and returns a list consisting of the elements split out from the string.

By default, the delimiter is set to a whitespace - so if you omit the delimiter argument, your string will be split on each whitespace.

Let's take a look at the behavior of the split[] method:

string = "Age,University,Name,Grades" lst = string.split[','] print[lst] print['Element types:', type[lst[0]]] print['Length:', len[lst]]

Our string had elements delimited with a comma, as in a CSV [comma-separated values] file, so we've set the delimieter appropriately.

This results in a list of elements of type str, no matter what other type they can represent:

['Age', 'University', 'Name', 'Grades'] Element types: Length: 4

Split String into List, Trim Whitespaces and Change Capitalization

Not all input strings are clean - so you won't always have a perfectly formatted string to split. Sometimes, strings may contain whitespaces that shouldn't be in the "final product" or have a mismatch of capitalized and non-capitalized letters.

Thankfully, it's pretty easy to process this list and each element in it, after you've split it:

string = "age, uNiVeRsItY, naMe, gRaDeS" lst = string.split[','] print[lst]

This results in:

['age', ' uNiVeRsItY', ' naMe', ' gRaDeS']

No good! Each element starts with a whitespace and the elements aren't properly capitalized at all. Applying a function to each element of a list can easily be done through a simple for loop so we'll want to apply a strip[]/trim[] [to get rid of the whitespaces] and a capitalization function.

Since we're not only looking to capitalize the first letter but also keep the rest lowercase [to enforce conformity], let's define a helper function for that:

def capitalize_word[string]: return string[:1].capitalize[] + string[1:].lower[]

The method takes a string, slices it on its first letter and capitalizes it. The rest of the string is converted to lowercase and the two changed strings are then concatenated.

We can now use this method in a loop as well:

string = "age, uNiVeRsItY, naMe, gRaDeS" lst = string.split[','] lst = [s.strip[] for s in lst] lst = [capitalize_word[s] for s in lst] print[lst] print['Element types:', type[lst[0]]] print['Length:', len[lst]]

This results in a clean:

['Age', 'University', 'Name', 'Grades'] Element types: Length: 4

Split String into List and Convert to Integer

What happens if you're working with a string-represented list of integers? After splitting, you won't be able to perform integer operations on these, since they're ostensibly be strings.

Thankfully, we can use the same for loop as before to convert the elements into integers:

string = "1,2,3,4" lst = string.split[','] lst = [int[s] for s in lst] print[lst] print['Element types:', type[lst[0]]] print['Length:', len[lst]]

Which now results in:

[1, 2, 3, 4] Element types: Length: 4

Split String into List with Limiter

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Besides the delimiter, the split[] method accepts a limiter - the number of times a split should occur.

It's an integer and is defined after the delimiter:

string = "Age, University, Name, Grades" lst = string.split[',', 2] print[lst]

Here, two splits occur, on the first and second comma, and no splits happen after that:

['Age', ' University', ' Name, Grades']

Conclusion

In this short guide, you've learned how to split a string into a list in Python.

You've also learned how to trim the whitespaces and fix capitalization as a simple processing step alongside splitting a string into a list.

In this tutorial, you’ll learn how to use Python to split a list into n chunks. You’ll learn how to split a Python list into chunks of size n, meaning that you’ll return lists that each contain n [or fewer if there are none left] items.

Lists in Python are mutable and heterogenous, meaning they can be changed and can contain items of different data types. They are also ordered, meaning their order matters and items can be accessed by their index position.

Knowing how to work with lists in Python is an import skill to learn. Here, you’ll learn how to use Python to split a list into chunks.

The Quick Answer: Use List Indexing

Use a for loop to split a Python list into chunks

How to Access a Python List by Its Index

One of the many wonderful properties of lists, is that they are ordered. This means that we can access an item, or a range of items, by its index. Let’s see how Python list indices work:

We can see here that Python lists have both a positive index as well as a negative index. A positive index begins at position 0, meaning the first item. A negative list index begins at -1, allowing you to retrieve the last item of a list.

Split Lists into Chunks Using a For-Loop

For-loops in Python are an incredibly useful tool to use. They make a lot of Python methods easy to implement, as well as easy to understand.

For this reason, let’s start off by using a for-loop to split our list into different chunks.

One of the ways you can split a list is into n different chunks. Let’s see how we can accomplish this by using a for loop:

# Split a Python List into Chunks using For Loops our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] chunked_list = list[] chunk_size = 3 for i in range[0, len[our_list], chunk_size]: chunked_list.append[our_list[i:i+chunk_size]] print[chunked_list] # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]

Let’s take a look at what we’ve done here:

  1. We instantiate two lists: our_list, which contains the items of our original list, and chunked_list, which is empty
  2. We also declare a variable, chunk_size, which we’ve set to three, to indicate that we want to split our list into chunks of size 3
  3. We then loop over our list using the range function. What we’ve done here is created items from 0, through to the size of our list, iterating at our chunk size. For example, our range function would read range[0, 11, 3], meaning that we’d loop over using items 0,3,6,9.
  4. We then index our list from i:i+chunk_size, meaning the first loop would be 0:3, then 3:6, etc.
  5. These indexed lists are appended to our list

We can see that this is a fairly straightforward way of breaking a Python list into chunks. Next, you’ll learn how to do accomplish this using Python list comprehensions.

Want to learn more? If you’d like to learn more about Python for-loops, check out my in-depth tutorial here, which will teach you all you need to know!

Split Python Lists into Chunks Using a List Comprehension

In many cases, Python for-loops can be rewritten in a more Pythonic way by writing them as one-liners called list comprehensions. List comprehensions in Python have a number of useful benefits over for-loops, including not having to instantiate an empty list first, and not having to break your for-loop over multiple lines.

To learn more about list comprehensions in Python, check out my YouTube tutorial below:

Learn all you need to know about Python list comprehensions!

Let’s see how we can write a Python list comprehension to break a list into chunks:

# Split a Python List into Chunks using list comprehensions our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] chunk_size = 3 chunked_list = [our_list[i:i+chunk_size] for i in range[0, len[our_list], chunk_size]] print[chunked_list] # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]

Before we break down this code, let’s see what the basic syntax of a Python list comprehension looks like:

How List Comprehensions work in Python

Now let’s break down our code to see how it works:

  1. We declare a variable chunk_size to determine how big we want our chunked lists to be
  2. For our list comprehensions expression, we index our list based on the ith to the i+chunk_sizeth position
  3. We use this expression to iterate over every item in the output of the range[] object that’s created based on range[0, len[our_list], chunk_size, which in this case would be 0,3,6,9

While this approach is a little faster to type, whether or not it is more readable than a for-loop, is up for discussion. Let’s learn how to split our Python lists into chunks using numpy.

Want to learn more? Check out my in-depth tutorial about Python list comprehensions by clicking here!

Split Lists into Chunks Using numpy

Numpy is an amazing Python library that makes mathematical operations significantly easier. That being said, numpy also works with a list-like object, called numpy arrays, that make working with lists much easier. These numpy arrays come packaged with lots of different methods to manipulate your arrays.

In this section of the tutorial, we’ll use the numpy array_split[] method to split our Python list into chunks.

Let’s see how we can use numpy to split our list:

# Split a Python List into Chunks using numpy import numpy as np our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] our_array = np.array[our_list] chunk_size = 3 chunked_arrays = np.array_split[our_array, len[our_list] // chunk_size + 1] chunked_list = [list[array] for array in chunked_arrays] print[chunked_list] # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]

This is a fairly long way of doing things, and we can definitely cut it down a little bit. Let’s see how that can be done:

chunked_list2 = [list[array] for array in np.array_split[np.array[our_list], len[our_list] // chunk_size + 1]] print[chunked_list2] # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]

Let’s break this down a little bit:

  1. We turn our list into a Numpy array
  2. We then declare our chunk size
  3. We then create chunked arrays by passing the array into the split_arrays method. As the second parameter, we add 1 to the floored division of the length of our array divided by our chunk size. The reason we do this is because the method technically splits our list into arrays of the size of the second parameter.
  4. Finally, we use a list comprehension to turn all the arrays in our list of arrays back into lists.

Want to learn more about division in Python? Check out my tutorial on how to use floored integer division and float division in Python in this tutorial here.

Let’s see how we can use itertools library to split a list into chunks. In particular, we can use the zip_longest function to accomplish this.

Let’s see how we can do this:

# Split a Python List into Chunks using itertools from itertools import zip_longest our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] chunk_size = 3 chunked_list = list[zip_longest[*[iter[our_list]]*chunk_size, fillvalue='']] print[chunked_list] # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, '']] chunked_list = [list[item] for item in list[zip_longest[*[iter[our_list]]*chunk_size, fillvalue='']]] print[chunked_list] # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]

We can see here that we can have a relatively simple implementation that returns a list of tuples. Notice one of the things that’s done here is split the list into chunks of size n, rather than into n chunks.

Conclusion

In this post, you learned how to split a Python list into chunks. You learned how to accomplish splitting a Python list into chunks of size n or into n number chunks. You learned how to do this using a for-loop, using list comprehensions, numpy and itertools.

To learn more about the numpy.array_split method, check out the official documentation here.

Video liên quan

Chủ Đề