Search

Python Functions That Add Items To Python Lists And Their Applications

In an earlier post, I discussed on how to remove items from python lists. Today, we will expand on the concept of lists and python list functions. We will be discussing on how to add items to python lists. In doing this, we are going to be using two functions, python’s append to list and python’s extend list functions. The two do the same job of adding objects to a list but they are not cousins. I mean, they are adding very different objects and use very different concepts.

python add to list
 

First we will start by discussing the python append to list function.

Python append to list function.

The syntax for this function is list.append(x). What it says is that you are going to be adding an item, x, to the end of the list. When you call the python append list function and give it an argument, x, where x is an object, it just adds x to the end of the list. Any object can be added to the end of the list, even sequences, but they are added just as one item.

Let us give examples of the python append to list function.

You can see from running the code above that no matter the length of the object being appended or the nature of the object, it is treated as a single item. So, whenever you want to add to a list and you want to treat that object as a single item in the list, you should use the python append to list function.

Python extend list function.

The syntax of the python extend list function is list.extend(iterable). The function takes an iterable and what it does is that it iterates through each of the items in the iterable and adds them to the list. It mutates the original list based on the number of items in the argument; the python extend list function is acting like a concatenating of the original list. Therefore, you could say that while in the python append to list function the length of the list increases by 1, in the python extend list function the list increases by the number of items in the iterable.

A picture is worth a thousand words. So, let’s illustrate the concept using examples.

I used the same examples for both the python append to list and python extend list functions just to help you better understand their functionality. You could see that for the cases, the python extend list function gives lists of longer length.

These two python list functions are not the only way you can add items to a python list. There are overloaded methods we could also use.

Using overloaded operators to add to python lists.

Operator overloading or function overloading is when the same built-in operator like + or * have different behaviors for different objects or classes. You might have noticed this earlier in your python journey. For example, adding integers, 2 + 3, gives a different behavior for the + operator from adding strings, ‘mango’ + ‘banana’.

We will discuss how the + and += operators are used to add iterables to lists. They are both semantically similar to the python extend list function. When using either of them, the second object must be an iterable, otherwise python will return an error.

Here is some code to show how these overloaded operators work.

So you now have at your arsenal methods to add items to python lists.

Happy pythoning.

To cap it all, note that the worst case complexity of the python append to list function is O(1) i.e constant complexity while that of python extend list is O(k) where k is the length of the iterable that is being used to extend the original list.

From Carbon Dioxide To Liquid Fuel With New, Cheaper, Efficient, And Innovative Technique

Human activity has been impacting the environment in negative ways. The greenhouse effect which produces climate change due to the trapping of the sunlight energy in the atmosphere is caused by having extra carbon dioxide in the atmosphere which is not removed by the photosynthetic processes of green plants. These extra carbon dioxide causes global warming. Climate change mitigation, or actions to reduce the magnitude and rate of global warming, along with climate change mitigation strategies, are now very popular. Several approaches have been proposed to remove these extra carbon dioxide or what is called carbon sequestration. Today, we will focus on a new innovation that not only promises to help the environment, but is commercially viable.

Scientists at the U.S Department of Energy’s (D.O.E) Argonne National Laboratory in collaboration with Northern Illinois University have undertaken research that have realized a way to not only remove carbon dioxide from the environment, but to also break it down and use it to manufacture ethanol.

Carbon dioxide to fuel
 

The discovery involves using catalysts, specifically electrocatalysts under low voltage. A catalyst is a substance that increases the rate of a chemical reaction without undergoing in the reaction itself and electrocatalysts are types of catalysts that function at electrode surfaces or they may be the electrode themselves. The electrocatalyst that was used by the researchers was copper, or atomically dispersed copper on carbon-powdered supports. These copper was used to break down trapped carbon-dioxide and water molecules and then these molecules were selectively reassembled into ethanol under an external electric field. When the efficiency was measured, it was found that the electrocatalytic selectivity process was 90 percent efficient, much better than existing techniques for converting carbon dioxide to ethanol. Furthermore, over extended periods of time it was found to operate under stable conditions at low voltages. The researchers also say that the costs for the process is also reasonable.

So one may ask: why convert carbon dioxide and water to ethanol? This is because ethanol is widely used in the U.S. It is used to produce gasoline and it is the chemical for many personal care and household products. Also, industries need ethanol to manufacture a host of products that provide lots of benefits to other industries and humans.

This is not the first time though that carbon dioxide will be converted into ethanol. But this method is more efficient and more cost-effective. Furthermore, the researchers say it is more stable than other previous methods. According to Tao Xu, a professor in physical chemistry and nanotechnology from Northern Illinois University, this process would open the doors to technology that would convert carbon dioxide electrocatalytically not only to ethanol, but to a vast array of other industrial chemicals.

So what are the benefits of removing carbon dioxide from the environment? The benefits are immense. Reusing carbon dioxide to manufacture ethanol would provide raw materials for industries making use of this ethanol at a cheaper cost. It reduces the increase in global temperatures. Presently, the world is working to make sure global temperatures do not exceed the two degrees Celsius mark. This approach will contribute its share. Also, greenhouse gases are being removed from the environment, helping to slow down climate change. Greta Thunberg, the Swedish teenage climate activist, would be happy to promote this technique. Also, since this approach has an efficient and reasonable cost, it will confer a lot of benefit to fossil fuel industries and alcohol fermentation plants who emit a lot of carbon dioxide annually into the atmosphere. They could derive some revenue by converting that carbon dioxide into ethanol. Furthermore, lots of jobs and career options would be created in the process in the U.S and around the world if this efficient technique is implemented.

The research has become successful that the researchers are in collaborative talks with industries to start producing ethanol. According to Di-Jia Liu, a senior chemist at Argonne’s Chemical Science and Engineering division, and one of the authors, they have plans to collaborate with industries in order to advance the promising technology. There are also plans to produce several other catalysts.

Material for this post was taken from Argonne National Laboratory press release.

Matched content