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

  • SEARCH
  • Home
  • 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 7880613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:11:14+00:00 2026-06-03T04:11:14+00:00

I have a python set set([1, 2, 3]) and always want to replace the

  • 0

I have a python set set([1, 2, 3]) and always want to replace the third element of the set with another value.

It can be done like below:

def change_last_elemnent(data):
    result = []
    for i,j in enumerate(list(data)):
        if i == 2:
            j = 'C'
        result.append(j)
    return set(result)

But is there any other pythonic way to do that,more smartly and making it more readable?

Thanks in advance.

  • 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-06-03T04:11:15+00:00Added an answer on June 3, 2026 at 4:11 am

    Sets are unordered, so the ‘third’ element doesn’t really mean anything. This will remove an arbitrary element.

    If that is what you want to do, you can simply do:

    data.pop()
    data.add(new_value)
    

    If you wish to remove an item from the set by value and replace it, you can do:

    data.remove(value) #data.discard(value) if you don't care if the item exists.
    data.add(new_value)
    

    If you want to keep ordered data, use a list and do:

    data[index] = new_value
    

    To show that sets are not ordered:

    >>> list({"dog", "cat", "elephant"})
    ['elephant', 'dog', 'cat']
    >>> list({1, 2, 3})
    [1, 2, 3]
    

    You can see that it is only a coincidence of CPython’s implementation that ‘3’ is the third element of a list made from the set {1, 2, 3}.

    Your example code is also deeply flawed in other ways. new_list doesn’t exist. At no point is the old element removed from the list, and the act of looping through the list is entirely pointless. Obviously, none of that really matters as the whole concept is flawed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set of value in float (always less than 0). Which I want
I have a python logger set up, using python's logging module. I want to
I have a Python regular expression that matches a set of filenames. How to
I have installed python 32 package to the C:\python32 I have also set the
I have managed to set up Cassandra + Thrift and the Python wrapper for
I have data that always looks something like this: alt text http://michaelfogleman.com/static/images/chart.png I need
I want to send email messages that have arbitrary unicode bodies in a Python
I have a C/C++ application in which I define a Python module. I set
I have a question about righteous way of programming in Python... Maybe there can
I have Python nested list that I'm trying to organize and eventually count number

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.