Python Concatenating 2 Lists


 
Thread Tools Search this Thread
Top Forums Programming Python Concatenating 2 Lists
# 1  
Old 01-20-2015
Python Concatenating 2 Lists

I am just trying to concatenate two lists together, but I am not sure what is wrong with my code, it won't run. Thank you for any help.

Code:
#!/usr/bin/python
# Takes two lists and returns a list that is the concatenation of both of
# them.

A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
B = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
def list_concat( A, B):

   print(A)
   print(B)
   A.extend(B)
   print(A)

# 2  
Old 01-21-2015
Quote:
Originally Posted by totoro125
I am just trying to concatenate two lists together, but I am not sure what is wrong with my code, it won't run. Thank you for any help.

Code:
#!/usr/bin/python
# Takes two lists and returns a list that is the concatenation of both of
# them.

A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
B = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
def list_concat( A, B):

   print(A)
   print(B)
   A.extend(B)
   print(A)

Your code is alright. It's just incomplete. You're not making any call to the function list_concat(). Call the function passing A and B as arguments and you will see the output.
# 3  
Old 03-11-2015
Code:
newList = list1 + list2

doesn't get easier
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Programming

Populating Lists in Def using Python

Dipping around in python again and need to create a def that will populate a list(content) with the files that os.walk finds from within this directory and then I will re.search through each files looking for content. In learning Python, can someone point me in the right direction. This is what I... (3 Replies)
Discussion started by: metallica1973
3 Replies

5. Shell Programming and Scripting

Separate Text File into Two Lists Using Python

Hello, I have a pretty simple question, but I am new to Python and am trying to write a simple program. Put simply, I want to take a text file that looks like this: 11111 22222 33333 44444 55555 66666 77777 88888 and produce two lists, one containing the contents of the left column, one the... (0 Replies)
Discussion started by: Tyler_92
0 Replies

6. Homework & Coursework Questions

[Python] Compare 2 lists

Hello, I'm new to the python programming, and I have a question. I have to write a program that prints a receipt for a restaurant. The input is a list which looks like: product1 product3 product8 .... In the other input file there is a list which looks like: product1 coffee 5,00... (1 Reply)
Discussion started by: dagendy
1 Replies

7. Programming

Python: Compare 2 word lists

Hi. I am trying to write a Python programme that compares two different text files which both contain a list of words. Each word has its own line worda wordb wordc I want to compare textfile 2 with textfile 1, and if there's a word in textfile 2 that is NOT in textfile 1, I want to... (6 Replies)
Discussion started by: Bloomy
6 Replies

8. Shell Programming and Scripting

need help in concatenating

Hi All , i`m writing a script , i stucked in middle . Script echo "Please Enter the INSTANCE name" read iName echo "The INSTANCE name is $iName" more /opt/IBMIHS*/conf/httpd.conf_"$iName" script end here i`m getting error as : Error /opt/IBMIHS*/conf/httpd.conf_w101:... (7 Replies)
Discussion started by: radha254
7 Replies

9. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies
Login or Register to Ask a Question