Use the extend() Method to Append a List Into Another List in Python. Python has a built-in method for lists named extend() that accepts an iterable as a parameter and adds it into the last position of the current iterable. Using it for lists will append the list parameter after the last element of the main list.
AddRange method in lists adds an entire collection of elements. Let us see an example − Firstly, set a list in C# and add elements − List<int> list = new List<int>(); list.Add(100); list.Add(200); list.Add(300); list.Add(400);
The items in the outer list are List<int> objects. In the first line, you create the outer list. In the second line, you create a list of int and add it as one of the items in the outer list. In the third line, you add an integer to the first inner list in the outer list.
GlobalStrings.AddRange(localStrings);
Note: You cannot declare the list object using the interface (IList).
Documentation: List<T>.AddRange(IEnumerable<T>)
.
GlobalStrings.AddRange(localStrings);
That works.
Documentation: List<T>.AddRange(IEnumerable<T>)
.
Try AddRange-method:
GlobalStrings.AddRange(localStrings);
With Linq
var newList = GlobalStrings.Append(localStrings)
Here is my example:
private List<int> m_machinePorts = new List<int>();
public List<int> machinePorts
{
get { return m_machinePorts; }
}
Init()
{
// Custom function to get available ethernet ports
List<int> localEnetPorts = _Globals.GetAvailableEthernetPorts();
// Custome function to get available serial ports
List<int> localPorts = _Globals.GetAvailableSerialPorts();
// Build Available port list
m_machinePorts.AddRange(localEnetPorts);
m_machinePorts.AddRange(localPorts);
}
if you want to get "terse" :)
List<string>GlobalStrings = new List<string>();
for(int x=1; x<10; x++) GlobalStrings.AddRange(new List<string> { "some value", "another value"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With