Is it possible to use the + operator to add more than two lists?

Question

In this exercise, the + operator is shown adding two lists together. Is it possible to combine more than two lists?

Answer

Yes, the + operator can be used to add more than two lists. It is possible to combine many lists together using the + operator just by adding more elements to the line. The following example shows how the + operator can join multiple lists mixing both variables and explicit lists.

list1 = [10, 20, 30]

list2 = [40, 50, 60]

list1 = list1 + list2 + [70, 80, 90] + [100, 110]
15 Likes

Hi . can we use minus(-) or multiply(*) symbol for removing or growing the list respectively.

If not what are the methods to remove an item from list

and what are methods to get repetition of list?

I have tried below mentioned code

list1 = range(14)

card_number = list(list1)

card_number = card_number - [0]

print(card_number)

it is showing error

1 Like

I try to google it and came accross the .remove() function. I dont know if this is the best way to go about it though. Anyway, thank you for the question, at least, we can add .remove() function to the little we know now

In [117]: my_list = ["samsung", "blackberry", "iphone", "motorola"]                                                               

In [118]: my_list.remove("blackberry")                                                                                            

In [119]: my_list                                                                                                                 
Out[119]: ['samsung', 'iphone', 'motorola']
8 Likes

I am little bit confuse between append() Function and + operator. What is the main Difference between them because we also add single element through + operator in a List and Append also work same manner. Any one can explain what is the main difference between them.

I am a freshman in this field but I think there is no main difference between .append() and plus (+) operator when you want to add a single element to a list. Just use what makes you comfortable.

Hopes it helps.

Yep, as mentioned by @core1320869428, .append() only lets you add a single item to the end of a list including another list as a single item. For example:

fruits = ['apples', 'oranges', peaches']
numbers = [1, 2, 3]
fruits.append(numbers)

print(fruits)

[‘apples’, ‘oranges’, ‘peaches’, [1, 2, 3]] -----> #Note that numbers is listed as a single item and not incorporated as individual items onto the fruits list.

The (+) operator allows you to add an existing list with other other items in a separate list to create a new variable storing the combined list. For example:

fruits = ['apples', 'oranges', 'peaches']
numbers = [1, 2, 3]
fruit_number = fruits + numbers

print(fruit_number)

[‘apples’, ‘oranges’, ‘peaches’, 1, 2, 3] #Note these are separate list items in a new list variable (fruit_number).

On a side note, if you want to extend an existing list similar to .append(), but with multiple, separate list items, then use the .extend() function or the += operator. For example:

fruits = ['apples', 'oranges', 'peaches']
numbers = [1, 2, 3]
fruits.extend(numbers) #or fruits += numbers

print(fruits)

[‘apples’, ‘oranges’, ‘peaches’, 1, 2, 3]---->#Note that the numbers list is incorporated into the existing fruits list as separate, list items.

6 Likes

Both (+) operator and extend() are doing pretty much the same thing. Is there any thing unique between their operation?
Thank you!

1 Like

If you’re concatenating two lists with + you are creating an entirely new list object.

lst1 = ["a", "b", "c"]
lst2 = [0, 1, 2]
lst_add = lst1 + lst2
# we now have references to three different list objects: lst1, lst2, lst_add
# extend modifies an exisiting list-
lst1.extend(["hat", "scarf", "cape"])
print(lst1)
Out: ['a', 'b', 'c', 'hat', 'scarf', 'cape']
# we have modified the original list.

Or: If you meant += and extend there are some subtle differences but you can ignore most of them for now. If you’re curious have a look at this SO discussion-

4 Likes