-3

To be clear I have found some similar questions, but (to the best of my knowledge) none of them are either concise enough or exactly what I am looking for.

I have a list(of ints) and two other ints, and want to make them all into one list. Right now I have:

old_list = [1, 2, 3]
first_int = 3
second_int = 5

new_list = old_list
new_list.append(first_int)
new_list.append(second_int) 

# new_list == [1, 2, 3, 3, 5]

I feel that there is a more Pythonic way to do this but I am unsure exactly how.
The order of 'new_list' is not important.

Thank you in advance.

0

1 Answer 1

4

You extend a list, for example:

new_list.extend([first_int, second_int])

By the way, your code also modifies old_list because new_list is just a reference to old_list.

If you really want a new list:

new_list = old_list[:]
Sign up to request clarification or add additional context in comments.

1 Comment

The code modifying old_list is a typo on my part. Before when I actually wanted a new list and not a reference to the previous one I've been using new_list = old_list.copy() Is there a difference between that and what you've shown?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.