python


Repeating elements in a list


I have a list with elements:
a = ['Monica', 'Mark', 'Nick', 'Margaret']
and I'd like to obtain the following list of names:
['Monica', 'Monica', 'Mark', 'Mark', 'Nick', 'Nick', 'Margaret', 'Margaret']
The simplest solution I came up with is:
lnames = []
for i, j in zip(a,a):
lnames.append(i)
lnames.append(j)
I've also tried to use a comprehension list:
lnames2 = [list(x) for x in zip(a,a)]
but in this case I'm getting:
[['Monica', 'Monica'], ['Mark', 'Mark'], ['Nick', 'Nick'], ['Margaret', 'Margaret']]
and don't know how to get the desire list from that point.
I'd like to ask you what are your solutions for this task.
Try this
def repeat(collection, count):
for x in collection:
for i in range(count):
yield x
a = ['Monica', 'Mark', 'Nick', 'Margaret']
print [x for x in repeat(a, 2)]
prints
['Monica', 'Monica', 'Mark', 'Mark', 'Nick', 'Nick', 'Margaret', 'Margaret']
Here's a working example:
import itertools
a = ['Monica', 'Mark', 'Nick', 'Margaret']
num_repeats = 2
lnames2 = list(itertools.chain(*[[x] * num_repeats for x in a]))
print(lnames2)

Related Links

Encoding issue for Python tool Unidecode on CL
Accept only numbers on a python string [closed]
scrapy spider pass parameters
sending data to particular client from twisted server
Type Error, object cannot be interpreted as an index with tk message box bind commands
What is this condition testing? [duplicate]
Testing Flask login and authentication?
Pyjnius import jar file
Regex - Combining an 'or' with a 'look-behind'
Command fails in python, but not in terminal
Does ndb.toplevel break transactions?
Unique variables names for results in a nested loop
Get unique set of adjacent vertices in python bulbs or gremlin?
Django admin project distribution and management
Increase event handling speed in Twisted?
thread and exception safe way of using HDFStore files

Categories

HOME
xbox-live
google-apps-script
cil
erlang
answer-set-programming
websphere
wildcard
cpu-architecture
wxwidgets
reportportal
mainframe
javamail
ll
ios10
simpy
pjsip
cakephp-2.9
gwtp
activesync
cloudflare
scala-native
uicollectionview
uiautomator
npm-install
java-ee-7
frp
reply
backup-strategies
idl
service-locator
precedence
sidr
pmwiki
vmd
haskell-pipes
android-preferences
stdclass
scalafx
z3py
tuleap
goquery
tiddlywiki
forerunnerdb
butterknife
sqldatareader
galleriffic
gameanalytics
uwp-maps
multilingual-app-toolkit
gmsmapview
faraday
date-format
sun-codemodel
cmocka
brackets-shell
android-recyclerview
direct3d12
wikitext
subresource-integrity
built-in
criteria-api
jazz
google-plus-one
chaining
ibmsbt
sortable
jstat
humanizer
coin-flipping
ssms-addin
data-generation
magic-numbers
json-patch
gfs
real-time-updates
appfog
xml-libxml
tridion2009
enumerators
userid
separation-of-concerns
nintendo
path-manipulation
electronic-signature
printing-web-page
hardware-acceleration
google-instant
chdatastructures
pci-bus
rtsp-client
evb
web-analytics-tools
visual-studio-dbpro
cots

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App