In this article, we will be looking at some of the many ways to create lists in Python. More advanced users may want to refer back for additional details as some concepts could apply beyond just creating lists.

We will start by taking a look at how you can add items directly into a list without defining or assigning an index variable first. Then, we will take a look at two common methods to generate new list elements.

Next, we will learn about another way to insert items into a list that calls upon the use of a special function. This function allows you to input any item as well as a starting position for the list. Finally, we will discuss using destructing to remove individual list elements.

This article will focus more on how to write your python scripts, but it is important to know the basics of constructing lists before moving onto longer projects.

Examples of unique values

show unique values in list python

One of the most common things list programmers do is to create a bullet point and then add some words under that bullet point. They may also include an image or two next to their sentence, or maybe even a link.

This practice is called creating a bullet point with an H1 tag. An H1 is a very powerful heading element because it can be used for many different purposes.

You may use it as a main title of a page, a sub-title, or even just to emphasize what item in a list you are talking about.

However, there is one thing that I have noticed people doing with bullets way too often.

It seems like everyone is using them to tell a story. A story with only one event usually has a short summary and a conclusion. That does not make sense here because we have three items!

So how do we solve this problem? We need to think about ways to modify our bullet points so they are more interesting and diverse.

I will go over several examples of ways to do this. You do not have to learn all of these techniques, but you should at least try one out every few days.

Calculate uniqueness

show unique values in list python

A way to determine how unique an element is within its collection is by calculating what is known as the Shannon entropy or information content of that item. This can be done using the built-in function entropy() in the statistics module.

The more bits of information you have, the less unique it is. The lower the information content, the higher the degree of uniqueness.

By taking the average information content across all items in a given set, we are able to get an overall measure of how unique each item is in the set.

This gives us our first metric for defining when something is too similar to other things in your set. If an item is not very unique, it will always appear in the set with some other item. As such, it won’t really contribute anything new to the set.

Create a unique list

The next thing you will do is create a new list! You can now add any element to this new list safely as it has an empty parent node.

To make things simpler, let’s start with our initial list we made before. We had `[1, 2]`.

Now that we have broken up our original list into two separate lists, what are we going to do with them?

We could just concatenate them together one after another like so:

`[1, 2].

Confirm uniqueness

show unique values in list python

The second way to check for duplicate entries is to confirm that each entry is unique. This can be done by using either of the two methods mentioned above or using another method.

The easiest way to do this is use the set() function. Add all items to a set, then see if there are any empty lists left. If not, some duplicates have been found!

Using sets as a way to check for dupes was discussed in our article about finding repeated strings. You will want to make sure you include both variables and functions when doing this concept.

Get the number of unique values

show unique values in list python

The next way to know how many unique elements there are in an array is using the length variable. This works similarly to how you calculate the size of an object by counting how many times it goes up or down, but instead for dimensions.

The difference here is that this one counts how many times each element appears in the given array. To do so, we use the args list which is assigned to every item when they are run individually.

This means that if you run print(len(“Hello”)) twice, it will output two because “Hello” has two instances in the array.

Now that we have our length variable set, all we need to do is add the length of the shorter array as a multiplier!

This makes sense because we want to know how many times each element appears inside of the longer array, thus taking into account both lengths. We can then subtract this value from the total amount of items in the longer array to get our answer.

Find the difference between two lists

show unique values in list python

In many cases, you will be given two sets of items or what are known as ‘lists’.

These can be either separated into different categories (i.e., fruits, vegetables) or one long list (like foods at a restaurant).

By this, we mean that there is an internal structure to each set. For example, if one batch of lists includes both names and numbers, then it would make sense to say that these lists contain both names AND numbers.

However, some lists may only include names or only include numbers. This is where things get tricky because how do you determine which differences are which?

There are several ways to approach this!

The easiest way is to use the commutative property. This says that no matter whether you switch the order of something and its complement, the result will still be true. So, for instance, let’s take the name-number pair mentioned before.

If your first item is a number, then their compliment is a name. If your second item is a name, then their compliment is a number.

Merge two lists into one

show unique values in list python

In creating our final list, we will need to merge two lists together. A common way to do this is using what’s called an empty list. You can think of it as a place to store the elements that are already in the second list.

In our case, we will be merging all of the names in firstList into the existing thirdList. ThirdList is going to get completely replaced with the content from firstList!

To make things easier, we will use the extension method Join() which takes your first argument (in our case, firstList) and adds each item in it as a new element in the second argument (secondArgument).

This extension method is defined in the System namespace, so you have to add that to access it.

returnedValue = [] # Create a variable for our joined list
for name in firstList:

Use a set instead of a list

show unique values in list python

A more efficient way to organize values is using a set. A set does not contain duplicated items, which means you can have multiple copies of an item without having to worry about it being there already.

A set also looks at most like a list, but with some important differences. The number of possible sets is much larger than that of lists, so if you have the same repeated element many times, sets are better suited for it.

By defining a key (or position) inside the set, we can determine whether an item has been added or not. This is called membership testing and allows us to know quickly if an item is part of the set or not!

This article will go over different ways to use sets in python and how to implement them.

Caroline Shaw is a blogger and social media manager. She enjoys blogging about current events, lifehacks, and her experiences as a millennial working in New York.