Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Add Elements to a List in Python?

#1
How to Add Elements to a List in Python?

In Python, there are always multiple ways to accomplish the same thing—but with subtle differences in the side effects. A great coder will always choose the most effective way for the problem at hand.

This tutorial shows you six different ways to add elements to a list in Python. In a nutshell, the different ways to add one or more elements to a list are:

  1. append(): add a single element to an existing list.
  2. extend(): add multiple elements to an existing list.
  3. insert(): add an element at an arbitrary position in an existing list.
  4. Slice assignment: replace a slice of an existing list.
  5. List concatenation with +: add one or more elements to a new list.
  6. Asterisk operator *: unpack multiple iterables into a new list.

Try It Yourself: Before we dive into each of those methods, let’s try them yourself in our interactive Python shell!

Exercise: Use each method to add yet another integer element 42 to each list. Which method is the best one to add multiple elements?

Next, you’ll learn about each method in a video tutorial and short example code snippet. I’ve written in-depth articles for each method so feel free to follow the references given in each method.

Method 1: append()




The list.append(x) method—as the name suggests—appends element x to the end of the list. You can call this method on each list object in Python. Here’s the syntax:

list.append(element)


Argument Description
element The object you want to append to the list.

# Method 1: append()
friends = ['Alice', 'Bob', 'Ann']
friends.append('Liz') print(friends)
# ['Alice', 'Bob', 'Ann', 'Liz']

Read More: Python List append() Method

Method 2: extend()




The list.extend(iter) method adds all elements in the argument iterable iter to an existing list. You can call this method on each list object in Python. Here’s the syntax:

list.extend(iterable)


Argument Description
iterable All the elements of the iterable will be added to the end of the list—in the order of their occurrence.

# Method 2: extend()
friends = ['Alice', 'Bob', 'Ann']
friends.extend(['Liz', 'Carl']) print(friends)
# ['Alice', 'Bob', 'Ann', 'Liz', 'Carl']

Read More: Python List extend() Method

Method 3: insert()




The list.insert(i, element) method adds an element element to an existing list at position i. All elements j>i will be moved by one index position to the right. You can call this method on each list object in Python. Here’s the syntax:

list.insert(index, element)


Argument Description
index Integer value representing the position before you want to insert an element
element Object you want to insert into the list.

# Method 3: insert()
friends = ['Alice', 'Bob', 'Ann']
friends.insert(3, 'Liz') print(friends)
# ['Alice', 'Bob', 'Ann', 'Liz']

Read More: Python List insert() Method

Method 4: Slice Assignment




Slice assignment is a little-used, beautiful Python feature to replace a slice with another sequence. Simply select the slice you want to replace on the left and the values to replace it on the right side of the equation. For example, the slice assignment list[2:4] = [42, 42] replaces the list elements with index 2 and 3 with the value 42.

# Method 4: slice assignment
friends = ['Alice', 'Bob', 'Ann']
friends[3:3] = ['Liz'] print(friends)
# ['Alice', 'Bob', 'Ann', 'Liz']

Read More: Python Slice Assignment

Method 5: List Concatenation with +




If you use the + operator on two integers, you’ll get the sum of those integers. But if you use the + operator on two lists, you’ll get a new list that is the concatenation of those lists.

# Method 5: list concatenation
friends = ['Alice', 'Bob', 'Ann']
friends = friends + ['Liz'] print(friends)
# ['Alice', 'Bob', 'Ann', 'Liz']

Read More: Python List Concatenation

Method 6: List Concatenation with Unpacking *




There are many applications of the asterisk operator. But one nice trick is to use it as an unpacking operator that “unpacks” the contents of a container data structure such as a list or a dictionary into another one.

# Method 6: unpacking
friends = ['Alice', 'Bob', 'Ann']
friends = [*friends, 'Liz'] print(friends)
# ['Alice', 'Bob', 'Ann', 'Liz']

A great advantage is that you can quickly unpack all elements of two or more list into a new list.

Read More: Python Unpacking with Asterisk

Python List Methods Cheat Sheet


Here’s your free PDF cheat sheet showing you all Python list methods on one simple page. Click the image to download the high-resolution PDF file, print it, and post it to your office wall:

Discussion


Let’s summarize the strengths and weaknesses of the different methods:

  1. Use the append() method to add a single element to an existing list without creating a new list.
  2. Use the extend() method to add multiple elements to an existing list without creating a new list.
  3. Use the insert() method to add an element at an arbitrary position in the list—without creating a new list.
  4. Use slice assignment to replace a slice of an existing list—without creating a new list.
  5. Use list concatenation with + to add one or more elements to a list—if you want to create a new list.
  6. Use the asterisk operator * to unpack multiple iterables into a new list—if you want to create a new list.

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2020/05/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python One Line Ternary xSicKxBot 0 2 7 hours ago
Last Post: xSicKxBot
  [Tut] Python One Line Quicksort xSicKxBot 0 2 Yesterday, 01:04 PM
Last Post: xSicKxBot
  [Tut] How to Create a List of Dictionaries in Python? xSicKxBot 0 2 07-09-2020, 11:37 AM
Last Post: xSicKxBot
  [Tut] Zip With List Output Instead of Tuple | Most Pythonic Way xSicKxBot 0 6 07-08-2020, 05:41 PM
Last Post: xSicKxBot
  [Tut] Create a List of Random Numbers — The Most Pythonic Way xSicKxBot 0 4 07-07-2020, 04:23 PM
Last Post: xSicKxBot
  [Tut] How to Create a Python List of Size n? xSicKxBot 0 5 07-06-2020, 12:09 PM
Last Post: xSicKxBot
  [Tut] Python SymPy — A Short Primer xSicKxBot 0 12 07-05-2020, 06:32 AM
Last Post: xSicKxBot
  [Tut] How to Sort a List of Tuples? – Most Pythonic Way! xSicKxBot 0 10 07-04-2020, 01:38 PM
Last Post: xSicKxBot
  [Tut] Python Tuple to Integer xSicKxBot 0 9 07-03-2020, 12:13 PM
Last Post: xSicKxBot
  [Tut] Python List max() xSicKxBot 0 12 07-02-2020, 11:01 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Upcoming Events

[-]
Discord

[-]
Latest Threads
[Tut] Python One Line Ternary
Last Post: xSicKxBot
Today 06:12 AM
» Replies: 0
» Views: 2
(Indie Deal) DY: Prologue FREEbie, Dirt ...
Last Post: xSicKxBot
Today 06:11 AM
» Replies: 0
» Views: 3
FNA–Now with Vulkan Support
Last Post: xSicKxBot
Today 06:11 AM
» Replies: 0
» Views: 3
Fedora - Use DNS over TLS
Last Post: xSicKxBot
Today 06:10 AM
» Replies: 0
» Views: 2
News - Nintendo Treehouse Live Airs Frid...
Last Post: xSicKxBot
Today 06:10 AM
» Replies: 0
» Views: 2
News - Elijah Wood Responds To Amazon's ...
Last Post: xSicKxBot
Today 06:10 AM
» Replies: 0
» Views: 2
Rust: The Best Keybinds for PRO players!
Last Post: SickProdigy
Yesterday 08:17 PM
» Replies: 0
» Views: 5
News - Here Are Japan’s Top Ten Best-Sel...
Last Post: xSicKxBot
Yesterday 08:17 PM
» Replies: 0
» Views: 2
News - This Week At Bungie – 7/09/2020
Last Post: xSicKxBot
Yesterday 03:03 PM
» Replies: 0
» Views: 2
Xbox Wire - Explore DC’s Wonder Woman Ex...
Last Post: xSicKxBot
Yesterday 03:02 PM
» Replies: 0
» Views: 2

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2019