How can you take a list of numbers and add 3 to every number in the list using only one line of code?

If the list is l1, we can create a new list l2 with all the numbers within being 3 more than each number in l2 by using a list comprehension as follows:

l2 = [x+3 for x in l1]

TD
Answered by Toby D. Python tutor

1854 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

What is the difference between a for loop and a while loop.


Write a program that takes a value x and then outputs x Fibonnaci numbers. E.g. if x=6 output would be 1 1 2 3 5 8


Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices .


What is the difference between a FOR loop and WHILE loop and how do I write them?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences