Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:00:29+00:00 2026-05-28T15:00:29+00:00

Please consider the two snippets of code (notice the distinction between string and integer):

  • 0

Please consider the two snippets of code (notice the distinction between string and integer):

a = []
a[:] = '1'

and

a = []
a[:] = 1

In the first case a is ['1']. In the second, I get the error TypeError: can only assign an iterable. Why would using '1' over 1 be fundamentally different here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:00:30+00:00Added an answer on May 28, 2026 at 3:00 pm

    Assigning to a slice requires an iterable on the right-hand side.

    '1' is iterable, while 1 is not. Consider the following:

    In [7]: a=[]
    
    In [8]: a[:]='abc'
    

    The result is:

    In [9]: a
    Out[9]: ['a', 'b', 'c']
    

    As you can see, the list gets each character of the string as a separate item. This is a consequence of the fact that iterating over a string yields its characters.

    If you want to replace a range of a‘s elements with a single scalar, simply wrap the scalar in an iterable of some sort:

    In [11]: a[:]=(1,) # single-element tuple
    
    In [12]: a
    Out[12]: [1]
    

    This also applies to strings (provided the string is to be treated as a single item and not as a sequence of characters):

    In [17]: a[:]=('abc',)
    
    In [18]: a
    Out[18]: ['abc']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the two snippets of code (the first prints Local eval, the second
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the two following snippets of code: (function f() { var x; try
Please consider the following two initialization methods. The first method simply passes the value
I am trying to replace one or two letters in a string. Please consider
This is a complex question, please consider carefully before answering. Consider this situation. Two
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
Please consider the following: http://jsfiddle.net/CASM6/9/ I have two rows of tabs. there are two
I need assistance in simulating movement between 2 points in a plane. Consider two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.