in Education by
E.x. listA = [9,10,11] listB = [12,13,14] Expected outcome: >>> concatenated list [9,10,11,12,13,14] Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
There are several methods to perform this task: Method 1: Just use the * operator to concentrate two lists in Python (Only works in Python 3.6+) Input:- # Initializing lists test_list1 = [9, 10, 11] test_list2 = [12, 13, 14] # using * operator to concat res_list = [*test_list1, *test_list2] # Printing concatenated list print ("Concatenated list using * operator : " + str(res_list)) Output:- [9,10,11,12,13,14] Method 2: You can use Native Method: You just have to traverse the second list and keep appending elements in the first list, this will result the elements in both lists and perform the append. Syntax: # concatenation using naive method # Initializing lists test_list1 = [ 9, 10, 11] test_list2 = [12, 13, 14] # using naive method to concat for i in test_list2 : test_list1.append(i) # Printing concatenated list print ("Concatenated list using naive method : " + str(test_list1)) Output: [9, 10, 11, 12, 13, 14] Method 3: Using extend() You can also use extend() to concatenate two lists in Python: Input: # Initializing lists test_list3 = [9, 10, 11] test_list4 = [12, 13, 14] # using list.extend() to concat test_list3.extend(test_list4) # Printing concatenated list print ("Concatenated list using list.extend() : " + str(test_list3)) Output: [9, 10, 11, 12, 13, 14] You can find more methods to perform the same command but this is the fastest, easiest and shortest method to perform it.

Related questions

0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    What's the difference? What are the advantages/disadvantages of tuples/lists? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have two lists in Python, like these: temp1 = ['One', 'Two', 'Three', 'Four'] temp2 = ['One' ... ways without cycles and checking? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I want my Python function to split a sentence (input) and store each word in a list. My current code splits ... the word print(words) Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    Imagine that you have: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way ... 42, 'food' : 'spam'} Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I am having two lists list1 = ['Apple', 'Orange', 'Grapes', 'Banana'] list2 = ['Apple', 'Orange'] I want to ... = ['Grapes', 'Banana'] Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have many rows in a database that contains XML and I'm trying to write a Python script that will go through ... the XML using Python? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I want an efficient way to append one string to another in Python. var1 = "foo" var2 = "bar" var3 = var1 + ... built-in method to use? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am getting an 'access is denied' error when I attempt to delete a folder that is not empty. I used the ... that is not empty? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How to copy a file in python? I already read this but found nothing. Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to freeze the dos pop-up, I already tried raw-input as mentioned in my study material of Python 2.6 and ... (3**200) raw_input() Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to use python to connect to a MySQL database, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
...