Add Multiple Items to a List

How to append multiple values to a list in Python

You can use the sequence method list.extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values.

>>> lst = [1, 2]
>>> lst.append(3)
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]

>>> lst.extend([5, 6, 7])
>>> lst.extend((8, 9, 10))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> lst.extend(range(11, 14))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

So you can use list.append() to append a single value, and list.extend() to append multiple values.

Add multiple items to a list

Code check:

This is offtopic here but the people over at CodeReview are more than happy to help you.

I strongly suggest you to do so, there are several things that need attention in your code.
Likewise I suggest that you do start reading tutorials since there is really no good reason not to do so.

Lists:

As you said yourself: you need a list of items. The way it is now you only store a reference to one item. Lucky there is exactly that to hold a group of related objects: a List.

Lists are very straightforward to use but take a look at the related documentation anyway.

A very simple example to keep multiple bikes in a list:

List<Motorbike> bikes = new List<Motorbike>();

bikes.add(new Bike { make = "Honda", color = "brown" });
bikes.add(new Bike { make = "Vroom", color = "red" });

And to iterate over the list you can use the foreach statement:

foreach(var bike in bikes) {
Console.WriteLine(bike.make);
}

How to insert multiple elements into a list?

To extend a list, you just use list.extend. To insert elements from any iterable at an index, you can use slice assignment...

>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[5:5] = range(10, 13)
>>> a
[0, 1, 2, 3, 4, 10, 11, 12, 5, 6, 7, 8, 9]

How to append multiple items in one line in Python

No. The method for appending an entire sequence is list.extend().

>>> L = [1, 2]
>>> L.extend((3, 4, 5))
>>> L
[1, 2, 3, 4, 5]

Adding multiple elements to a list in one line C#

To answer your exact question:

Look into the List.AddRange(...) function: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.addrange?view=netframework-4.7.2

The syntax would look like this:

Toppings.AddRange(new List<EToppings>() {top1, top2, top3, top4});

To resolve the underlying issue:

Creating many different constructors that only have a difference in the number of extra "topping" parameters is a bit messy. You could/should condense all of those into a single constructor with variable length arguments through the params keyword in C#.

public Pizza(ECrust crust, EPizzaSize size, params EToppings[] toppings) : this()
{
Crust = crust;
Price = Cost[size];
Size = size;
Toppings.AddRange(toppings);
}

Adding multiple elements to a list in Python

The first thing you should do in your function is initialize an empty list. Then you can loop the correct number of times, and within the for loop you can append into myList. You should also avoid using eval and in this case simply use int to convert to an integer.

def userNum(iterations):
myList = []
for _ in range(iterations):
value = int(input("Enter a number for sound: "))
myList.append(value)
return myList

Testing

>>> userNum(5)
Enter a number for sound: 2
Enter a number for sound: 3
Enter a number for sound: 1
Enter a number for sound: 5
Enter a number for sound: 9
[2, 3, 1, 5, 9]

Efficiently adding multiple items to a list/collection after creation VB.net without an add line for each item

Try this:

Dim list As New List(Of String) From { "string1", "string2", "string3" }

Here's some documentation on Collection Initializers.

It's better than the existing answer, because the existing answer creates an actual array with its own separate memory, and then adds the array to the list. The loop is still there, it's just hidden by the AddRange() method. This collection initializer syntax also just hides the loop, but at least the extra array object is saved.

how to add multiple items in list for a fix dictionary key

So you want a single element in the dictionary with the key being set to "X" and the value set to the list of students?

var students = data
.Select(d => new Student
{
Name = d.Key.Split('!')[0],
Section = d.Key.Split('!')[1],
Age = d.Value
})
.ToList();

finalResult.Add(dictKey, students);


Related Topics



Leave a reply



Submit