0

Example I have attributes as below:

a = [1,2,3]
b = [3,2,1]
r1 = 5
r2 = 6

How do I get:

foo = [1,2,3,3,2,1,5,6]
2
  • foo = a + b + [r1] + [r2] Commented Sep 24, 2015 at 2:13
  • 3
    possible duplicate of Python - append vs. extend Commented Sep 24, 2015 at 2:15

3 Answers 3

2

@falsetru As simple as:

foo = a + b + [r1, r2]
Sign up to request clarification or add additional context in comments.

2 Comments

You can't add an int to a list.
Why don't you do a + b + [r1, r2]?
1
def combine(*args):
    result = []
    for arg in args:
        if type(arg) is int:
            result.append(arg)
        elif type(arg) is list:
            result += arg
    return result

Comments

1

You have many options:

How @falsetru said:

foo = a + b + [r1] + [r2]

Or:

foot = []
foot.extend(a)
foot.extend(b)
foot.append(r1)
foot.append(r2)

Or:

foot = []
foot.extend(a)
foot.extend(b)
foot.extend([r1])
foot.extend([r2])

Or:

foot = []
foot.extend(a + b + [r1] + [r2])

You can know more about the lists here: Python Data Structures

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.