Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I do it easier ?
#1
I have a list. I need to change my list so that the first ten elems is becoming with a capital letter. I'm doing it this way:
numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thriteen', 'fourteen', 'fivteen']
first_ten = numbers[:10]

capitalize_first_ten =
for x in first_ten:
    capitalize_first_ten.append(x.capitalize())

tmp = list_[10:]
print(list_)
print(capitalize_first_ten + tmp)
I'm sure there are a better way than mine. How would you make it ?
Reply
#2
capitalized = [word.capitalize() for word in numbers]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I reduced the code.
numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thriteen', 'fourteen', 'fivteen']
tmp = [x.capitalize() for x numbers[:10]]
numbers = tmp + numbers[:10]
print(numbers)
Reply
#4
numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thriteen', 'fourteen', 'fivteen']
>>> print([x.capitalize() for x in numbers[:10] * 2])
['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten']
Or it's okay that way if want 1 capitalize with and 1 with lower case.
Reply
#5
Example with islice and chain from itertools:

from itertools import islice
from itertools import chain

new_list = list(chain((x.capitalize() for x in islice(numbers, 0, 10)), islice(numbers, 10)))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
capitalized = [word.capitalize() if numbers.index(word) < 10 else word for word in numbers]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Jul-31-2017, 02:12 PM)wavic Wrote: capitalized = [word.capitalize() if numbers.index(word) < 10 else word for word in numbers]
I think it's better like this
capitalized = [word.capitalize() if i < 10 else word for i, word in enumerate(numbers)]
Reply
#8
(Jul-31-2017, 03:45 PM)buran Wrote:
(Jul-31-2017, 02:12 PM)wavic Wrote: capitalized = [word.capitalize() if numbers.index(word) < 10 else word for word in numbers]
I think it's better like this
capitalized = [word.capitalize() if i < 10 else word for i, word in enumerate(numbers)]
 

I was lazy
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For loop to make it easier Amirdst 2 2,055 Apr-28-2020, 04:12 PM
Last Post: Yoriz
  Easier way to manage dependencies? Tylersuard 2 2,351 Jan-01-2020, 09:19 PM
Last Post: Tylersuard
  making the code easier, list comprehension go127a 2 2,059 May-26-2019, 06:19 PM
Last Post: Gribouillis
  [ tools atom v pycharm ] -- when it comes to easier learning, and making life easier, oneofakindpython 2 3,361 Jun-18-2017, 08:19 AM
Last Post: 3cxmostar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020